using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Timers;
using System.Collections;
using YukiFX5017.Core;
using Yuki.Sockets;
using Yuki.Sockets.Encryptions;

namespace YukiFX5017
{
    public class Client
    {
        public uint UID;
        public string Account = "";
        public string Password = "";
        public byte Authentication;
        public YukiSocketClient MySocket;
        public Character MyChar;
        public bool Online = true;
        public uint Status;
        public int CurrentNPC = 0;
        public bool There = false;
        public string Ip = "";
        public int RGemId = 0;
        public int RItemype = 0;
        public int ChColor = 0;
        public byte GemId = 0;
        public string RBGem = "";
        public string CharName1 = "Empty";
        public string CharName2 = "Empty";
        public string CharName3 = "Empty";
        public string CharName4 = "Empty";
        public string CharName5 = "Empty";
        public int CharacterUsage = 0;

        public Client(YukiSocketClient _Socket)
        {
            MySocket = _Socket;
        }
        public void LoadChar()
        {
            Send(General.MyPackets.PlacePacket1(MyChar));
            Send(General.MyPackets.PlacePacket2(MyChar));
            Send(General.MyPackets.PlacePacket3(MyChar));
            Send(General.MyPackets.LogonPacket());
            Send(General.MyPackets.ShowMinimap(true));
            Send(General.MyPackets.GeneralData((long)(MyChar.UID), 3, 0, 0, 96));
            Send(General.MyPackets.CharacterInfo(MyChar));
            Send(General.MyPackets.Vital(MyChar.UID, 26, MyChar.GetStat()));
            MyChar.StartXPCircle();

            MyChar.PKMode = 3;

            World.SpawnMeToOthers(MyChar, false);
            World.SpawnOthersToMe(MyChar, false);
            World.SurroundNPCs(MyChar, false);
            World.SurroundMobs(MyChar, false);
            World.SurroundDroppedItems(MyChar, false);
            MyChar.UnPackInventory();
            MyChar.SendInventory();
            MyChar.UnPackEquips();
            MyChar.SendEquips(true);
            MyChar.UnPackSkills();
            MyChar.SendSkills();
            MyChar.UnPackProfs();
            MyChar.SendProfs();
            MyChar.UnPackWarehouses();
            MyChar.UnPackEnemies();
            MyChar.UnPackFriends();

            foreach (DictionaryEntry DE in Guilds.AllGuilds)
            {
                Guild AGuild = (Guild)DE.Value;
                Send(General.MyPackets.GuildName(AGuild.GuildID, AGuild.GuildName));
            }
            if (MyChar.MyGuild != null)
            {
                Send(General.MyPackets.GuildInfo(MyChar.MyGuild, MyChar));
                Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, MyChar.MyGuild.Bulletin, 2111));
                Send(General.MyPackets.GuildName(MyChar.GuildID, MyChar.MyGuild.GuildName));
                Send(General.MyPackets.GeneralData(MyChar.UID, 0, 0, 0, 97));
            }

            if (MyChar.RBCount == 2)
            {
                Send(General.MyPackets.String(MyChar.UID, 10, "2NDMetempsychosis"));
            }
            if (MyChar.Rank == 7)
                Send(General.MyPackets.String(MyChar.UID, 10, "letter1"));
            if (MyChar.Rank == 6)
                Send(General.MyPackets.String(MyChar.UID, 10, "letter2"));
            if (MyChar.Rank == 5)
                Send(General.MyPackets.String(MyChar.UID, 10, "letter3"));
            if (MyChar.Rank == 4)
                Send(General.MyPackets.String(MyChar.UID, 10, "letter4"));
            if (MyChar.Rank == 3)
                Send(General.MyPackets.String(MyChar.UID, 10, "letter5"));
            if (MyChar.Rank == 2)
            {
                Send(General.MyPackets.String(MyChar.UID, 10, "coronet3"));
                Send(General.MyPackets.String(MyChar.UID, 10, "letter6"));
            }
            if (MyChar.Rank == 1)
            {
                Send(General.MyPackets.String(MyChar.UID, 10, "coronet4"));
                Send(General.MyPackets.String(MyChar.UID, 10, "letter7"));
            }
            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "[Sphinx Chronicles Online]", 2000));
            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "We are currently under developement. You may see things being added", 2000));
            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "as time goes on. Until then, please follow the thread on ePVP or our forum.", 2000));
            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "Sphinx Revision: 1.0.1a", 2000));
            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "Players Online: " + Kernel.Chars.Count, 2005));
            Send(General.MyPackets.GeneralData(MyChar.UID, 0, 0, 0, 75));
            There = true;
            General.WriteLine("Succesfully login to server. (" + MyChar.Name + " - " + Account + "[" + Ip + "]" + ")");
            World.SpawnMeToOthers(MyChar, true);
            Kernel.Chars.Remove(this.UID);
            Kernel.Chars.Add(this.UID, this.MyChar);
            World.SpawnMeToOthers(MyChar, false);
            MyChar.Teleport(MyChar.LocMap, MyChar.LocX, MyChar.LocY);
        }
        public unsafe void GetPacket(byte[] Data)
        {
            ushort PacketId = (ushort)((Data[3] << 8) | Data[2]);
            int PacketType;

            #region guilds
            switch (PacketId)
            {
                case 1052:
                    {
                        this.UID = BitConverter.ToUInt32(Data, 8);
                        uint Key2 = BitConverter.ToUInt32(Data, 4);
                        (this.MySocket.Crypto as ConquerStanderedCipher).SetKeys((uint)this.UID, Key2);
                        
                        foreach (string File in System.IO.Directory.GetFiles(DataBase.DBPath + @"\Accounts"))
                        {
                            try
                            {
                                IniFile I = new IniFile(File);
                                if (UID == Convert.ToUInt32(I.IniReadString("Account", "UID")))
                                {
                                    this.Account = I.IniReadString("Account", "Username").ToString();
                                    this.Password = I.IniReadString("Account", "Password").ToString();

                                    this.CharName1 = I.IniReadString("Account", "CharName1").ToString();
                                    this.CharName2 = I.IniReadString("Account", "CharName2").ToString();
                                    this.CharName3 = I.IniReadString("Account", "CharName3").ToString();
                                    this.CharName4 = I.IniReadString("Account", "CharName4").ToString();
                                    this.CharName5 = I.IniReadString("Account", "CharName5").ToString();

                                    if (I.IniReadString("Account", "LogonType") == "2")
                                    {
                                        try
                                        {
                                            this.CharacterUsage = Convert.ToInt32(I.IniReadString("Account", "SaveSlot"));
                                            this.Send(General.MyPackets.NewCharPacket(this.UID));
                                            General.WriteLine("Account: " + this.Account + " is attempting to create a character");
                                        }
                                        catch
                                        {
                                            General.WriteLine("Error in attempt of character creation");
                                        }
                                    }
                                    else if (I.IniReadString("Account", "LogonType") == "1")
                                    {
                                        this.MyChar = new Character();
                                        this.MyChar.UID = this.UID;
                                        this.CharacterUsage = 0;
                                        this.MyChar.Name = "CharacterSelecter";
                                        this.MyChar.MyClient = this;
                                        this.Send(General.MyPackets.LanguageResponse(UID));
                                        this.Send(General.MyPackets.CharacterInfo(this.MyChar));
                                        this.Send(General.MyPackets.AfterChar());
                                        this.CurrentNPC = 1;
                                        NPCDialogues.NPCTalk(1, 0, this);
                                        break;
                                    }
                                    else
                                    {
                                        this.MySocket.Disconnect();
                                    }
                                }
                                else
                                {
                                    //this.MySocket.Disconnect();
                                }
                            }
                            catch (Exception Exc)
                            {
                                General.WriteLine(Convert.ToString(Exc));
                            }
                        }
                        break;
                    }
                case 1024:
                    {
                        if (MyChar.StatP > 0)
                        {
                            MyChar.StatP--;
                            MyChar.Str += Data[4];
                            MyChar.Agi += Data[5];
                            MyChar.Vit += Data[6];
                            MyChar.Wis += Data[7];
                        }
                        break;
                    }
                case 2050:
                    {
                        if (Data[4] == 3 && MyChar.CPs >= 5 && World.Broadcast == true)
                        {
                            MyChar.CPs -= 5;
                            byte Len = Data[13];
                            string Message = "";
                            for (int i = 0; i < Len; i++)
                            {
                                Message += Convert.ToChar(Data[14 + i]);
                            }
                            World.SendMsgToAll(Message, MyChar.Name, 2500);
                            General.BrCastMSG();
                            World.BroadcastSend = false;
                        }
                        else
                        {
                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "Please wait untill, the old broadcast time is done.", 2000));
                        }
                        break;
                    }
                case 1112://See guild member's status
                    {
                        string Name = "";
                        int Pos = 9;
                        while (Pos < Data.Length)
                        {
                            Name += Convert.ToChar(Data[Pos]);
                            Pos++;
                        }
                        break;
                    }
                case 1015://Send guild members
                    {
                        if (MyChar.MyGuild == null)
                            return;
                        string Total = "";
                        string OnlineMembers = "";
                        string OfflineMembers = "";
                        byte Count = 1;

                        string[] Splitter = MyChar.MyGuild.Creator.Split(':');
                        if (Kernel.Chars.ContainsKey(uint.Parse(Splitter[1])))
                            OnlineMembers += Convert.ToChar((Splitter[0] + Convert.ToChar(32) + Splitter[2] + Convert.ToChar(32) + "1").Length) + (Splitter[0] + Convert.ToChar(32) + Splitter[2] + Convert.ToChar(32) + "1");
                        else
                            OfflineMembers += Convert.ToChar((Splitter[0] + Convert.ToChar(32) + Splitter[2] + Convert.ToChar(32) + "0").Length) + (Splitter[0] + Convert.ToChar(32) + Splitter[2] + Convert.ToChar(32) + "0");


                        foreach (DictionaryEntry DE in MyChar.MyGuild.DLs)
                        {
                            string DL = (string)DE.Value;
                            Count++;
                            Splitter = DL.Split(':');
                            if (Kernel.Chars.ContainsKey(uint.Parse(Splitter[1])))
                                OnlineMembers += Convert.ToChar((Splitter[0] + Convert.ToChar(32) + Splitter[2] + Convert.ToChar(32) + "1").Length) + (Splitter[0] + Convert.ToChar(32) + Splitter[2] + Convert.ToChar(32) + "1");
                            else
                                OfflineMembers += Convert.ToChar((Splitter[0] + Convert.ToChar(32) + Splitter[2] + Convert.ToChar(32) + "0").Length) + (Splitter[0] + Convert.ToChar(32) + Splitter[2] + Convert.ToChar(32) + "0");
                        }

                        foreach (DictionaryEntry DE in MyChar.MyGuild.Members)
                        {
                            string NM = (string)DE.Value;
                            Count++;
                            Splitter = NM.Split(':');
                            if (Kernel.Chars.ContainsKey(uint.Parse(Splitter[1])))
                                OnlineMembers += Convert.ToChar((Splitter[0] + Convert.ToChar(32) + Splitter[2] + Convert.ToChar(32) + "1").Length) + (Splitter[0] + Convert.ToChar(32) + Splitter[2] + Convert.ToChar(32) + "1");
                            else
                                OfflineMembers += Convert.ToChar((Splitter[0] + Convert.ToChar(32) + Splitter[2] + Convert.ToChar(32) + "0").Length) + (Splitter[0] + Convert.ToChar(32) + Splitter[2] + Convert.ToChar(32) + "0");
                        }

                        Total = OnlineMembers + OfflineMembers;

                        Send(Data);
                        Send(General.MyPackets.StringGuild(11, 11, Total, Count));

                        break;
                    }
                case 1107:
                    {
                        byte Type = Data[4];
                        switch (Type)
                        {
                            case 1://Join guild request
                                {
                                    if (MyChar.MyGuild == null)
                                    {
                                        uint UID = BitConverter.ToUInt32(Data, 8);

                                        Character JoinWho = Kernel.Chars[UID];
                                        if (JoinWho.MyGuild != null && JoinWho.GuildPosition == 100 || JoinWho.GuildPosition == 90)
                                            JoinWho.MyClient.Send(General.MyPackets.SendGuild(MyChar.UID, 1));
                                    }

                                    break;
                                }
                            case 2://Accept join request
                                {
                                    uint UID = BitConverter.ToUInt32(Data, 8);
                                    if (Kernel.Chars.ContainsKey(UID))
                                    {
                                        Character WhoJoins = Kernel.Chars[UID];
                                        if (WhoJoins.MyGuild == null)
                                        {
                                            WhoJoins.GuildID = MyChar.MyGuild.GuildID;
                                            WhoJoins.MyGuild = MyChar.MyGuild;
                                            WhoJoins.GuildPosition = 50;
                                            MyChar.MyGuild.PlayerJoins(WhoJoins);
                                            World.UpdateSpawn(WhoJoins);

                                            WhoJoins.MyClient.Send(General.MyPackets.GuildName(WhoJoins.GuildID, WhoJoins.MyGuild.GuildName));
                                            WhoJoins.MyClient.Send(General.MyPackets.GuildInfo(WhoJoins.MyGuild, WhoJoins));
                                        }
                                    }
                                    break;
                                }
                            case 3://Leave the guild
                                {
                                    if (MyChar.MyGuild != null && MyChar.GuildPosition != 100)
                                    {
                                        Send(General.MyPackets.SendGuild(MyChar.MyGuild.GuildID, 19));
                                        MyChar.MyGuild.PlayerQuits(MyChar);
                                        MyChar.GuildDonation = 0;
                                        MyChar.GuildID = 0;
                                        MyChar.GuildPosition = 0;
                                        MyChar.MyGuild = null;
                                        World.UpdateSpawn(MyChar);
                                        World.SpawnOthersToMe(MyChar, false);
                                    }
                                    break;
                                }
                            case 12://Guild status
                                {
                                    if (MyChar.MyGuild != null)
                                    {
                                        Send(General.MyPackets.GuildName(MyChar.GuildID, MyChar.MyGuild.GuildName));
                                        Send(General.MyPackets.GuildInfo(MyChar.MyGuild, MyChar));
                                        Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, MyChar.MyGuild.Bulletin, 2111));
                                        Send(General.MyPackets.GeneralData(MyChar.UID, 0, 0, 0, 97));
                                    }
                                    break;
                                }
                            case 11://Donate
                                {
                                    if (MyChar.MyGuild != null)
                                    {
                                        uint Amount = BitConverter.ToUInt32(Data, 8);

                                        if (MyChar.Silvers >= Amount)
                                        {
                                            MyChar.MyGuild.Fund += Amount;
                                            MyChar.Silvers -= Amount;
                                            MyChar.GuildDonation += Amount;
                                            MyChar.MyGuild.Refresh(MyChar);
                                            Send(General.MyPackets.GuildInfo(MyChar.MyGuild, MyChar));
                                            World.SendMsgToAll(MyChar.Name + " has donated " + Amount.ToString() + " silvers to " + MyChar.MyGuild.GuildName + ".", "SYSTEM", 2005);
                                        }
                                    }

                                    break;
                                }
                        }
                        break;
                    }
            #endregion
                #region friends
                case 1019:
                    {
                        uint UID = BitConverter.ToUInt32(Data, 4);
                        byte Type = Data[8];
                        switch (Type)
                        {
                            case 14:
                                {
                                    World.SendMsgToAll(MyChar.Name + " has broken friendship with " + (string)MyChar.Friends[UID], "SYSTEM", 2005);
                                    MyChar.Friends.Remove(UID);
                                    Send(General.MyPackets.FriendEnemyPacket(UID, "", 14, 0));

                                    if (Kernel.Chars.ContainsKey(UID))
                                    {
                                        Character Char = Kernel.Chars[UID];
                                        if (Char != null && Char.MyClient != null)
                                        {
                                            Char.MyClient.Send(General.MyPackets.FriendEnemyPacket(MyChar.UID, "", 14, 0));
                                            Char.Friends.Remove(MyChar.UID);
                                        }
                                        else
                                            DataBase.RemoveFromFriend(MyChar.UID, UID);
                                    }
                                    else
                                        DataBase.RemoveFromFriend(MyChar.UID, UID);

                                    break;
                                }
                            case 10:
                                {
                                    Character Other = Kernel.Chars[UID];
                                    if (Other == null)
                                        return;
                                    if (Other.RequestFriendWith == MyChar.UID)
                                    {
                                        MyChar.AddFriend(Other);
                                        Other.AddFriend(MyChar);
                                        World.SendMsgToAll(MyChar.Name + " and " + Other.Name + " are friends from now on.", "SYSTEM", 2005);
                                    }
                                    else
                                    {
                                        MyChar.RequestFriendWith = UID;
                                        Other.MyClient.Send(General.MyPackets.SendMsg(Other.MyClient.UID, "SYSTEM", Other.Name, MyChar.Name + " requested to add friends.", 2005));
                                    }
                                    break;
                                }
                        }

                        break;
                    }
                #endregion
                #region trades
                case 1056:
                    {
                        uint UID = BitConverter.ToUInt32(Data, 4);

                        byte Type = Data[8];

                        switch (Type)
                        {
                            case 1://Request trade
                                {
                                    Character Who = Kernel.Chars[UID];
                                    if (UID != MyChar.TradingWith)
                                    {
                                        if (Status != 0 && There == true && !MyChar.Name.StartsWith("(PvP)"))
                                        {
                                            Who.MyClient.Send(General.MyPackets.TradePacket(MyChar.UID, 1));
                                            MyChar.TradingWith = UID;
                                            Who.TradingWith = MyChar.UID;
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "[Trade]Request for trading has been sent out.", 2005));
                                        }
                                    }
                                    else
                                    {
                                        if (Status != 0 && There == true && !MyChar.Name.StartsWith("(PvP)"))
                                        {
                                            Who.Trading = true;
                                            MyChar.Trading = true;
                                            Who.MyClient.Send(General.MyPackets.TradePacket(MyChar.UID, 3));
                                            Send(General.MyPackets.TradePacket(Who.UID, 3));
                                        }
                                    }
                                    break;
                                }
                            case 2://Close trade
                                {
                                    if (Status != 0 && There == true && !MyChar.Name.StartsWith("(PvP)"))
                                    {
                                        if (MyChar.Trading)
                                        {
                                            Character Who = Kernel.Chars[MyChar.TradingWith];
                                            if (Who != null)
                                            {
                                                Who.MyClient.Send(General.MyPackets.TradePacket(MyChar.TradingWith, 5));
                                                Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "Trading failed!", 2005));
                                                Who.MyClient.Send(General.MyPackets.SendMsg(Who.MyClient.UID, "SYSTEM", Who.Name, "Trading failed!", 2005));
                                                Who.Trading = false;
                                                MyChar.Trading = false;
                                                Who.TradingWith = 0;
                                                MyChar.TradingWith = 0;
                                                foreach (uint iuid in MyChar.MyTradeSide)
                                                {
                                                    string Item = MyChar.FindItem(iuid);
                                                    string[] Splitter = Item.Split('-');
                                                    Send(General.MyPackets.AddItem(iuid, int.Parse(Splitter[0]), byte.Parse(Splitter[1]), byte.Parse(Splitter[2]), byte.Parse(Splitter[3]), byte.Parse(Splitter[4]), byte.Parse(Splitter[5]), 0, int.Parse(Splitter[6]), int.Parse(Splitter[7])));
                                                }
                                                foreach (uint iuid in Who.MyTradeSide)
                                                {
                                                    string Item = Who.FindItem(iuid);
                                                    string[] Splitter = Item.Split('-');
                                                    Who.MyClient.Send(General.MyPackets.AddItem(iuid, int.Parse(Splitter[0]), byte.Parse(Splitter[1]), byte.Parse(Splitter[2]), byte.Parse(Splitter[3]), byte.Parse(Splitter[4]), byte.Parse(Splitter[5]), 0, int.Parse(Splitter[6]), int.Parse(Splitter[7])));
                                                }
                                                MyChar.MyTradeSide = new ArrayList(20);
                                                Who.MyTradeSide = new ArrayList(20);
                                                MyChar.TradingCPs = 0;
                                                MyChar.TradingSilvers = 0;
                                                MyChar.TradeOK = false;
                                                Who.TradeOK = false;
                                                Who.MyTradeSideCount = 0;
                                                MyChar.MyTradeSideCount = 0;
                                            }
                                        }
                                    }

                                    break;
                                }
                            case 6://Add an item
                                {
                                    if (Status != 0 && There == true && !MyChar.Name.StartsWith("(PvP)"))
                                    {
                                        Character Who = Kernel.Chars[MyChar.TradingWith];
                                        if (Who.ItemsInInventory + MyChar.MyTradeSideCount < 40)
                                        {
                                            string Item = MyChar.FindItem(UID);
                                            Who.MyClient.Send(General.MyPackets.TradeItem(UID, Item));
                                            MyChar.MyTradeSide.Add(UID);
                                            MyChar.MyTradeSideCount++;
                                        }
                                        else
                                        {
                                            Send(General.MyPackets.TradePacket(UID, 11));
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "[Trade]Your trade partner can't hold any more items.", 2005));
                                        }
                                    }
                                    break;
                                }
                            case 7://Specify money
                                {
                                    if (Status != 0 && There == true && !MyChar.Name.StartsWith("(PvP)"))
                                    {
                                        if (MyChar.Silvers >= UID)
                                        {
                                            MyChar.TradingSilvers = UID;
                                            Character Who = Kernel.Chars[MyChar.TradingWith];
                                            Who.MyClient.Send(General.MyPackets.TradePacket(UID, 8));
                                        }
                                    }
                                    break;
                                }
                            case 10://OK
                                {
                                    if (Status != 0 && There == true && !MyChar.Name.StartsWith("(PvP)"))
                                    {
                                        Character Who = Kernel.Chars[MyChar.TradingWith];


                                        if (Who.Silvers < Who.TradingSilvers || Who.CPs < Who.TradingCPs || MyChar.Silvers < MyChar.TradingSilvers || MyChar.CPs < MyChar.TradingCPs || MyChar.Silvers < MyChar.TradingSilvers || MyChar.CPs < MyChar.TradingCPs || Who.Silvers < Who.TradingSilvers || Who.CPs < Who.TradingCPs)
                                        {
                                        }
                                        else
                                        {
                                            if (Who.TradeOK)
                                            {
                                                Who.MyClient.Send(General.MyPackets.TradePacket(MyChar.TradingWith, 5));
                                                Send(General.MyPackets.TradePacket(MyChar.UID, 5));
                                                foreach (uint itemuid in Who.MyTradeSide)
                                                {
                                                    string item = Who.FindItem(itemuid);
                                                    MyChar.AddItem(item, 0, itemuid);
                                                    Who.DropRemoveItem(itemuid);
                                                }
                                                foreach (uint itemuid in MyChar.MyTradeSide)
                                                {
                                                    string item = MyChar.FindItem(itemuid);
                                                    Who.AddItem(item, 0, itemuid);
                                                    MyChar.DropRemoveItem(itemuid);
                                                }



                                                if (Who.CPs >= Who.TradingCPs)
                                                    MyChar.CPs += Who.TradingCPs;
                                                if (MyChar.CPs >= MyChar.TradingCPs)
                                                    Who.CPs += MyChar.TradingCPs;
                                                if (MyChar.CPs >= MyChar.TradingCPs)
                                                    MyChar.CPs -= MyChar.TradingCPs;
                                                if (Who.CPs >= Who.TradingCPs)
                                                    Who.CPs -= Who.TradingCPs;

                                                if (Who.Silvers >= Who.TradingSilvers)
                                                    MyChar.Silvers += Who.TradingSilvers;
                                                if (MyChar.Silvers >= MyChar.TradingSilvers)
                                                    Who.Silvers += MyChar.TradingSilvers;
                                                if (MyChar.Silvers >= MyChar.TradingSilvers)
                                                    MyChar.Silvers -= MyChar.TradingSilvers;
                                                if (Who.Silvers >= Who.TradingSilvers)
                                                    Who.Silvers -= Who.TradingSilvers;



                                                Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "Trading succeeded.", 2005));
                                                Who.MyClient.Send(General.MyPackets.SendMsg(Who.MyClient.UID, "SYSTEM", Who.Name, "Trading succeeded.", 2005));

                                                MyChar.Trading = false;
                                                MyChar.TradingWith = 0;
                                                MyChar.MyTradeSideCount = 0;
                                                MyChar.TradeOK = false;
                                                MyChar.MyTradeSide = new ArrayList(20);
                                                MyChar.TradingCPs = 0;
                                                MyChar.TradingSilvers = 0;

                                                Who.MyTradeSide = new ArrayList(20);
                                                Who.TradingCPs = 0;
                                                Who.TradingSilvers = 0;
                                                Who.TradeOK = false;
                                                Who.TradingWith = 0;
                                                Who.Trading = false;
                                                Who.MyTradeSideCount = 0;
                                                Console.WriteLine("Save Reason : (Trade) [" + MyChar.Name + " & " + Who.Name + "]");
                                            }
                                            else
                                            {
                                                MyChar.TradeOK = true;
                                                Who.MyClient.Send(General.MyPackets.TradePacket(0, 10));
                                            }
                                        }
                                    }
                                    break;
                                }
                            case 13://Specify CPs
                                {
                                    if (Status != 0 && There == true && !MyChar.Name.StartsWith("(PvP)"))
                                    {
                                        MyChar.TradingCPs = UID;
                                        Character Who = Kernel.Chars[MyChar.TradingWith];
                                        Who.MyClient.Send(General.MyPackets.TradePacket(UID, 12));
                                    }
                                    break;
                                }
                        }

                        break;
                    }
                #endregion
                #region teams
                case 1023:
                    {
                        byte Type = Data[4];
                        switch (Type)
                        {
                            case 0://Create
                                {
                                    MyChar.TeamLeader = true;
                                    MyChar.MyTeamLeader = MyChar;
                                    Send(General.MyPackets.Vital(MyChar.UID, 26, MyChar.GetStat()));
                                    Send(General.MyPackets.TeamPacket(MyChar.UID, 0));
                                    World.UpdateSpawn(MyChar);
                                    break;
                                }
                            case 1://Join request
                                {
                                    uint JoinWho = BitConverter.ToUInt32(Data, 4); //(uint)(Data[8] + (Data[9] << 8) + (Data[10] << 16) + (Data[11] << 24));
                                    Character Who = Kernel.Chars[JoinWho];
                                    if (Who.TeamLeader)
                                    {
                                        if (!Who.JoinForbidden)
                                        {
                                            if (Who.PlayersInTeam < 4)
                                            {
                                                Who.MyClient.Send(General.MyPackets.TeamPacket(MyChar.UID, 1));
                                                Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "[Team]Request to join team has been sent out.", 2005));
                                            }
                                            else
                                                Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "[Team]The team is full.", 2005));
                                        }
                                        else
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "The team doesn't accept new members.", 2005));
                                    }
                                    else
                                        Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "[Team]The target has not created a team.", 2005));

                                    break;
                                }
                            case 2://Exit team
                                {
                                    MyChar.MyTeamLeader.TeamRemove(MyChar, false);
                                    break;
                                }
                            case 3://I accept invitation
                                {
                                    uint WhoInvited = BitConverter.ToUInt32(Data, 4); //(uint)(Data[8] + (Data[9] << 8) + (Data[10] << 16) + (Data[11] << 24));
                                    Character Who = Kernel.Chars[WhoInvited];
                                    if (Who != null)
                                        Who.TeamAdd(MyChar);

                                    break;
                                }
                            case 4://Invite request
                                {
                                    uint InviteWho = BitConverter.ToUInt32(Data, 4); //(uint)(Data[8] + (Data[9] << 8) + (Data[10] << 16) + (Data[11] << 24));
                                    Character Invited = Kernel.Chars[InviteWho];
                                    if (!Invited.TeamLeader && Invited.MyTeamLeader == null && MyChar.TeamLeader && MyChar.PlayersInTeam < 4)
                                    {
                                        Invited.MyClient.Send(General.MyPackets.TeamPacket(MyChar.UID, 6));
                                        Invited.MyClient.Send(General.MyPackets.TeamPacket(MyChar.UID, 4));
                                    }

                                    break;
                                }
                            case 5://I accept your join request
                                {
                                    uint WhoJoins = BitConverter.ToUInt32(Data, 4);// (uint)(Data[8] + (Data[9] << 8) + (Data[10] << 16) + (Data[11] << 24));
                                    Character Joiner = Kernel.Chars[WhoJoins];

                                    MyChar.TeamAdd(Joiner);

                                    break;
                                }
                            case 6://Dismiss
                                {
                                    MyChar.TeamDismiss();
                                    break;
                                }
                            case 7://Kick
                                {
                                    uint KickWho = BitConverter.ToUInt32(Data, 4); //(uint)(Data[8] + (Data[9] << 8) + (Data[10] << 16) + (Data[11] << 24));
                                    Character Kicked = Kernel.Chars[KickWho];

                                    MyChar.TeamRemove(Kicked, true);
                                    break;
                                }
                            case 8://Forbid joining
                                {
                                    MyChar.JoinForbidden = true;
                                    break;
                                }
                            case 9://UnForbid joining
                                {
                                    MyChar.JoinForbidden = false;
                                    break;
                                }
                        }
                        break;
                    }
                #endregion
                #region warehouse
                case 1102:
                    {
                        uint NPCID = BitConverter.ToUInt32(Data, 4); //(uint)((Data[7] << 24) + (Data[6] << 16) + (Data[5] << 8) + (Data[4]));
                        uint ItemUID = BitConverter.ToUInt32(Data, 12);// (uint)((Data[15] << 24) + (Data[14] << 16) + (Data[13] << 8) + (Data[12]));
                        byte Type = Data[8];
                        byte WHID = 0;

                        if (NPCID == 8)
                            WHID = 0;
                        else if (NPCID == 81)
                            WHID = 1;
                        else if (NPCID == 82)
                            WHID = 2;
                        else if (NPCID == 83)
                            WHID = 3;
                        else if (NPCID == 84)
                            WHID = 4;
                        else if (NPCID == 85)
                            WHID = 5;


                        if (Type == 0)
                        {
                            Send(General.MyPackets.WhItems(MyChar, WHID, (ushort)NPCID));
                        }
                        else if (Type == 1)//Throw an item into warehouse
                        {
                            if (WHID == 0 && MyChar.TCWHCount < 20 || WHID == 1 && MyChar.PCWHCount < 20 || WHID == 2 && MyChar.ACWHCount < 20 || WHID == 3 && MyChar.DCWHCount < 20 || WHID == 4 && MyChar.BIWHCount < 20 || WHID == 5 && MyChar.MAWHCount < 40)
                            {
                                string Item = MyChar.FindItem(ItemUID);
                                MyChar.RemoveItem(ItemUID);
                                MyChar.AddWHItem(Item, ItemUID, WHID);
                                Send(General.MyPackets.WhItems(MyChar, WHID, (ushort)NPCID));
                            }
                        }
                        else if (Type == 2)//Take an item from warehouse
                        {
                            if (MyChar.ItemsInInventory < 40)
                            {
                                string Item = MyChar.FindWHItem(ItemUID, WHID);
                                //"1088000-0-0-0-0-0"
                                MyChar.RemoveWHItem(ItemUID);
                                Send(General.MyPackets.WhItems(MyChar, WHID, (ushort)NPCID));
                                MyChar.AddItem(Item, 0, ItemUID);
                            }
                        }

                        break;
                    }
                #endregion
                #region addingpluses
                case 2036:
                    {
                        uint MainUID = BitConverter.ToUInt32(Data, 8);// (uint)((Data[11] << 24) + (Data[10] << 16) + (Data[9] << 8) + Data[8]);
                        uint Minor1UID = BitConverter.ToUInt32(Data, 12);// (uint)((Data[15] << 24) + (Data[14] << 16) + (Data[13] << 8) + Data[12]);
                        uint Minor2UID = BitConverter.ToUInt32(Data, 16);// (uint)((Data[19] << 24) + (Data[18] << 16) + (Data[17] << 8) + Data[16]);
                        uint Gem1 = (uint)((Data[23] << 24) + (Data[22] << 16) + (Data[21] << 8) + Data[20]);
                        uint Gem2 = (uint)((Data[24] << 24) + (Data[23] << 16) + (Data[22] << 8) + Data[21]);

                        string MainItem = MyChar.FindItem(MainUID);
                        string Minor1Item = MyChar.FindItem(Minor1UID);
                        string Minor2Item = MyChar.FindItem(Minor2UID);

                        if (MainItem == null || Minor1Item == null || Minor2Item == null)
                            return;

                        byte MainPlus = 0;
                        byte Minor1Plus = 0;
                        byte Minor2Plus = 0;
                        uint MainItemId = 0;
                        uint Minor1ItemId = 0;
                        uint Minor2ItemId = 0;

                        string[] Splitter;
                        string[] MainItemE;
                        MainItemE = MainItem.Split('-');
                        MainPlus = byte.Parse(MainItemE[1]);
                        MainItemId = uint.Parse(MainItemE[0]);
                        Splitter = Minor1Item.Split('-');
                        Minor1Plus = byte.Parse(Splitter[1]);
                        Minor1ItemId = uint.Parse(Splitter[0]);
                        Splitter = Minor2Item.Split('-');
                        Minor2Plus = byte.Parse(Splitter[1]);
                        Minor2ItemId = uint.Parse(Splitter[0]);

                        if (Minor1Plus == Minor2Plus)
                            if (Minor1Plus == MainPlus || MainPlus == 0 && Minor1Plus == 1)
                                if (Other.ItemType2(Minor1ItemId) == Other.ItemType2(Minor2ItemId) && Other.ItemType2(Minor2ItemId) == Other.ItemType2(MainItemId) || (Other.ItemType(MainItemId) == 4 && Other.ItemType(Minor1ItemId) == 4 && Other.ItemType(Minor2ItemId) == 4) || Other.ItemType(MainItemId) == 5 && Other.ItemType(Minor1ItemId) == 5 && Other.ItemType(Minor2ItemId) == 5)
                                {
                                    MainPlus++;
                                    MyChar.RemoveItem(Minor1UID);
                                    MyChar.RemoveItem(Minor2UID);
                                    MyChar.RemoveItem(MainUID);
                                    MyChar.AddItem(MainItemE[0] + "-" + MainPlus + "-" + MainItemE[2] + "-" + MainItemE[3] + "-" + MainItemE[4] + "-" + MainItemE[5], 0, MainUID);
                                    return;
                                }

                        if (Minor1Plus == Minor2Plus)
                            if (Minor1Plus == MainPlus || MainPlus == 0 && Minor1Plus == 1)
                                if (Other.ItemType2(Minor1ItemId) == 73 || Other.ItemType2(Minor2ItemId) == 73)
                                    if ((Other.ItemType2(Minor1ItemId) == 73 && Other.ItemType2(Minor2ItemId) == 73) || (Other.ItemType2(Minor1ItemId) == 73 && Other.ItemType2(Minor2ItemId) != 73 && Other.ItemType2(MainItemId) == Other.ItemType2(Minor2ItemId)) || (Other.ItemType2(Minor2ItemId) == 73 && Other.ItemType2(Minor1ItemId) != 73 && Other.ItemType2(MainItemId) == Other.ItemType2(Minor1ItemId)))
                                    {
                                        MainPlus++;
                                        MyChar.RemoveItem(Minor1UID);
                                        MyChar.RemoveItem(Minor2UID);
                                        MyChar.RemoveItem(MainUID);
                                        MyChar.AddItem(MainItemE[0] + "-" + MainPlus + "-" + MainItemE[2] + "-" + MainItemE[3] + "-" + MainItemE[4] + "-" + MainItemE[5], 0, MainUID);
                                    }

                        break;
                    }
                #endregion
                #region money
                case 1101:
                    {
                        if (MyChar.ItemsInInventory > 39)
                            return;
                        uint ItemUID = BitConverter.ToUInt32(Data, 4); //(uint)((Data[7] << 24) + (Data[6] << 16) + (Data[5] << 8) + Data[4]);
                        DroppedItem TehItem = null;
                        if (Kernel.AllDroppedItems.ContainsKey(ItemUID))
                            TehItem = Kernel.AllDroppedItems[ItemUID];

                        if (TehItem != null)
                        {
                            if (TehItem.Money == 0)
                                MyChar.AddItem(TehItem.Item, 0, ItemUID);
                            else
                            {
                                MyChar.Silvers += TehItem.Money;
                                Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "You have picked up " + TehItem.Money + " silvers.", 2005));
                            }
                            Kernel.AllDroppedItems.Remove(ItemUID);
                            World.ItemDissappears(TehItem);
                        }
                        break;
                    }
                #endregion
                #region attack
                case 1022:
                    {

                        int AttackType = BitConverter.ToInt32(Data, 20);//(Data[23] << 24) + (Data[22] << 16) + (Data[21] << 8) + (Data[20]);
                        if (DateTime.Now < MyChar.LastTargetting.AddMilliseconds(255))
                            return;

                        MyChar.LastTargetting = DateTime.Now;

                        if (AttackType == 21)
                        {
                            MyChar.PTarget = null;
                            MyChar.TGTarget = null;
                            MyChar.MobTarget = null;
                            MyChar.Attacking = false;
                            ushort SkillId = Convert.ToUInt16(((long)Data[24] & 0xFF) | (((long)Data[25] & 0xFF) << 8));
                            SkillId ^= (ushort)0x915d;
                            SkillId ^= (ushort)MyChar.UID;
                            SkillId = (ushort)(SkillId << 0x3 | SkillId >> 0xd);
                            SkillId -= 0xeb42;

                            long x = (Data[16] & 0xFF) | ((Data[17] & 0xFF) << 8);
                            long y = (Data[18] & 0xFF) | ((Data[19] & 0xFF) << 8);

                            x = x ^ (uint)(MyChar.UID & 0xffff) ^ 0x2ed6;
                            x = ((x << 1) | ((x & 0x8000) >> 15)) & 0xffff;
                            x |= 0xffff0000;
                            x -= 0xffff22ee;

                            y = y ^ (uint)(MyChar.UID & 0xffff) ^ 0xb99b;
                            y = ((y << 5) | ((y & 0xF800) >> 11)) & 0xffff;
                            y |= 0xffff0000;
                            y -= 0xffff8922;

                            uint Target = ((uint)Data[12] & 0xFF) | (((uint)Data[13] & 0xFF) << 8) | (((uint)Data[14] & 0xFF) << 16) | (((uint)Data[15] & 0xFF) << 24);
                            Target = ((((Target & 0xffffe000) >> 13) | ((Target & 0x1fff) << 19)) ^ 0x5F2D2463 ^ MyChar.UID) - 0x746F4AE6;

                            if (SkillId != 1110 && SkillId != 1015 && SkillId != 1020 && SkillId != 1025)
                            {
                                if (MyChar.LocMap == 1039 || SkillId == 1002 || SkillId == 1000 && SkillId != 1110 && SkillId != 1015 && SkillId != 1020 && SkillId != 1025)
                                {
                                    MyChar.SkillLooping = SkillId;
                                    MyChar.SkillLoopingX = (ushort)x;
                                    MyChar.SkillLoopingY = (ushort)y;
                                    MyChar.SkillLoopingTarget = Target;
                                    MyChar.AtkType = 21;
                                    MyChar.Attack();
                                    MyChar.Attacking = true;
                                }
                                else
                                    MyChar.UseSkill(SkillId, (ushort)x, (ushort)y, Target);
                            }
                            else
                                MyChar.UseSkill(SkillId, (ushort)x, (ushort)y, Target);
                        }
                        if (AttackType == 8)//Marriage Proposal
                        {
                            fixed (byte* Ptr = Data)
                            {
                                uint TargetUID = *(uint*)(Ptr + 12);
                                Character Target = Kernel.Chars[TargetUID];
                                *(uint*)(Ptr + 8) = MyChar.UID;
                                {
                                    if (MyChar.Model == 2001 || MyChar.Model == 2002)
                                    {

                                        if (Target.Spouse == "" || Target.Spouse == "None")
                                        {
                                            Target.MyClient.Send(Data);
                                        }
                                        else
                                        {
                                            Send(General.MyPackets.NPCSay(Target.Name + " has already got an Spouse."));
                                            Send(General.MyPackets.NPCLink("Ok.", 255));
                                            Send(General.MyPackets.NPCSetFace(30));
                                            Send(General.MyPackets.NPCFinish());
                                        }
                                    }
                                    if (MyChar.Model == 1003 || MyChar.Model == 1004)
                                    {
                                        if (Target.Spouse == "" || Target.Spouse == "None")
                                        {
                                            Target.MyClient.Send(Data);
                                        }
                                        else
                                        {
                                            Send(General.MyPackets.NPCSay(Target.Name + " has already got an Spouse."));
                                            Send(General.MyPackets.NPCLink("Ok.", 255));
                                            Send(General.MyPackets.NPCSetFace(30));
                                            Send(General.MyPackets.NPCFinish());
                                        }
                                    }
                                }
                            }
                        }
                        if (AttackType == 9)//Marriage Accept
                        {
                            fixed (byte* Ptr = Data)
                            {
                                uint UID = *(uint*)(Ptr + 12);
                                Character Char = Kernel.Chars[UID];
                                {

                                    Char.Spouse = MyChar.Name;
                                    MyChar.Spouse = Char.Name;
                                    Send(General.MyPackets.CharacterInfo(MyChar));
                                    World.SendMsgToAll(Char.Name + " and " + MyChar.Name + " has been united as husband and wife!", "LoveStone", 2011);
                                }
                            }
                        }
                        if (AttackType == 2 || AttackType == 25)
                        {
                            uint Target = Data[0x0f];
                            Target = (Target << 8) | Data[0x0e];
                            Target = (Target << 8) | Data[0x0d];
                            Target = (Target << 8) | Data[0x0c];
                            MyChar.TargetUID = Target;

                            if (Target < 7000 && Target >= 5000)
                            {
                                Npc ThisTGO = Kernel.Npcs[Target];
                                MyChar.MobTarget = null;

                                MyChar.AtkType = (byte)AttackType;
                                MyChar.TGTarget = ThisTGO;

                            }
                            else if (Target > 400000 && Target < 500000)
                            {
                                if (MyChar.Guard != null)
                                {
                                    if (Target != MyChar.Guard.UID)
                                    {
                                        Mob TargetMob = Kernel.Mobs[Target];

                                        MyChar.MobTarget = null;

                                        if (TargetMob != null)
                                        {
                                            MyChar.AtkType = (byte)AttackType;
                                            MyChar.MobTarget = TargetMob;
                                        }
                                    }
                                }
                                else
                                {
                                    Mob TargetMob = Kernel.Mobs[Target];

                                    MyChar.MobTarget = null;

                                    if (TargetMob != null)
                                    {
                                        MyChar.AtkType = (byte)AttackType;
                                        MyChar.MobTarget = TargetMob;
                                    }
                                }
                            }
                            else
                            {
                                foreach (var DE in Kernel.Chars)
                                {
                                    Character ThisChar = DE.Value;
                                    if (ThisChar.UID == Target)
                                    {
                                        MyChar.PTarget = ThisChar;
                                        MyChar.AtkType = (byte)AttackType;
                                        break;
                                    }
                                }
                            }
                            if (MyChar.AtkType == 2 && MyChar.PTarget != null && MyChar.PTarget.Flying)
                                MyChar.PTarget = null;

                            MyChar.Attacking = true;
                            MyChar.Attack();
                        }
                        
                        break;
                    }
                #endregion
                #region gemsocketting
                case 1027:
                    {

                        uint MainItemUID = BitConverter.ToUInt32(Data, 8);//(uint)((Data[11] << 24) + (Data[10] << 16) + (Data[9] << 8) + Data[8]);
                        uint GemUID = BitConverter.ToUInt32(Data, 12);//(uint)((Data[15] << 24) + (Data[14] << 16) + (Data[13] << 8) + Data[12]);
                        string RealItem = "";
                        string RealGem = "";

                        int Mode = Data[18];
                        int Slot = Data[16];
                        int Counter = 0;

                        if (Mode == 0)
                        {
                            if (GemUID != 0)
                            {
                                foreach (ulong uid in MyChar.Inventory_UIDs)
                                {
                                    if (GemUID == uid)
                                        RealGem = MyChar.Inventory[Counter];
                                    Counter++;
                                }
                            }
                            Counter = 0;

                            if (MainItemUID != 0)
                            {
                                foreach (ulong uid in MyChar.Inventory_UIDs)
                                {
                                    if (MainItemUID == uid)
                                        RealItem = MyChar.Inventory[Counter];
                                    Counter++;
                                }
                            }

                            if (RealItem != "")
                                if (RealGem != "")
                                {
                                    string[] ItemParts = RealItem.Split('-');
                                    string[] GemParts = RealGem.Split('-');
                                    if (Slot == 1)
                                    {
                                        ItemParts[4] = Convert.ToString(uint.Parse(GemParts[0]) - 700000);
                                    }
                                    if (Slot == 2)
                                    {
                                        ItemParts[5] = Convert.ToString(uint.Parse(GemParts[0]) - 700000);
                                    }
                                    MyChar.RemoveItem(MainItemUID);
                                    MyChar.RemoveItem(GemUID);
                                    MyChar.AddItem(ItemParts[0] + "-" + ItemParts[1] + "-" + ItemParts[2] + "-" + ItemParts[3] + "-" + ItemParts[4] + "-" + ItemParts[5], 0, MainItemUID);
                                }
                        }
                        else
                        {
                            if (MainItemUID != 0)
                            {
                                foreach (ulong uid in MyChar.Inventory_UIDs)
                                {
                                    if (MainItemUID == uid)
                                        RealItem = MyChar.Inventory[Counter];
                                    Counter++;
                                }
                            }
                            if (RealItem == null)
                                return;
                            string[] ItemParts = RealItem.Split('-');
                            if (Slot == 1)
                                if (ItemParts[4] != "0")
                                {
                                    if (ItemParts[5] == "0" || ItemParts[5] == "255")
                                        ItemParts[4] = "255";
                                    else if (ItemParts[5] != "0")
                                    {
                                        ItemParts[4] = ItemParts[5];
                                        ItemParts[5] = "255";
                                    }
                                }
                            if (Slot == 2)
                                if (ItemParts[5] != "0")
                                {
                                    ItemParts[5] = "255";
                                }
                            MyChar.RemoveItem(MainItemUID);
                            MyChar.AddItem(ItemParts[0] + "-" + ItemParts[1] + "-" + ItemParts[2] + "-" + ItemParts[3] + "-" + ItemParts[4] + "-" + ItemParts[5], 0, MainItemUID);
                        }

                        if (Data.Length == 40)
                        {
                            MainItemUID = BitConverter.ToUInt32(Data, 28);//(uint)((Data[31] << 24) + (Data[30] << 16) + (Data[29] << 8) + Data[28]);
                            GemUID = BitConverter.ToUInt32(Data, 32);//(uint)((Data[35] << 24) + (Data[34] << 16) + (Data[33] << 8) + Data[32]);
                            RealItem = "";
                            RealGem = "";

                            Mode = Data[38];
                            Slot = Data[36];
                            Counter = 0;

                            if (Mode == 0)
                            {
                                if (GemUID != 0)
                                {
                                    foreach (ulong uid in MyChar.Inventory_UIDs)
                                    {
                                        if (GemUID == uid)
                                            RealGem = MyChar.Inventory[Counter];
                                        Counter++;
                                    }
                                }
                                Counter = 0;

                                if (MainItemUID != 0)
                                {
                                    foreach (ulong uid in MyChar.Inventory_UIDs)
                                    {
                                        if (MainItemUID == uid)
                                            RealItem = MyChar.Inventory[Counter];
                                        Counter++;
                                    }
                                }

                                if (RealItem != "")
                                    if (RealGem != "")
                                    {
                                        string[] ItemParts = RealItem.Split('-');
                                        string[] GemParts = RealGem.Split('-');
                                        if (Slot == 1)
                                        {
                                            ItemParts[4] = Convert.ToString(uint.Parse(GemParts[0]) - 700000);
                                        }
                                        if (Slot == 2)
                                        {
                                            ItemParts[5] = Convert.ToString(uint.Parse(GemParts[0]) - 700000);
                                        }
                                        MyChar.RemoveItem(MainItemUID);
                                        MyChar.RemoveItem(GemUID);
                                        MyChar.AddItem(ItemParts[0] + "-" + ItemParts[1] + "-" + ItemParts[2] + "-" + ItemParts[3] + "-" + ItemParts[4] + "-" + ItemParts[5], 0, MainItemUID);
                                    }
                            }
                            else
                            {
                                string[] ItemParts = RealItem.Split('-');
                                if (Slot == 1)
                                {
                                    ItemParts[4] = "0";
                                }
                                if (Slot == 2)
                                {
                                    ItemParts[5] = "0";
                                }
                                MyChar.RemoveItem(MainItemUID);
                                MyChar.AddItem(ItemParts[0] + "-" + ItemParts[1] + "-" + ItemParts[2] + "-" + ItemParts[3] + "-" + ItemParts[4] + "-" + ItemParts[5], 0, MainItemUID);
                            }
                        }
                        
                        break;
                    }
                #endregion
                #region npctalk
                case 2031:
                    {
                        int NPCID = BitConverter.ToInt32(Data, 4);//(Data[7] << 24) + (Data[6] << 16) + (Data[5] << 8) + Data[4];
                        int Control = (int)Data[10];
                        CurrentNPC = NPCID;
                        NPCDialogues.NPCTalk(CurrentNPC, Control, this);
                        break;
                    }
                case 2032:
                    {
                        int Control = (int)Data[10];
                        if (CurrentNPC != 0 && Control != 0)
                            NPCDialogues.NPCTalk(CurrentNPC, Control, this);
                        break;
                    }
                #endregion
                #region movement
                case 1005:
                    {
                        sbyte AddX = 0;
                        sbyte AddY = 0;
                        byte Dir = (byte)(Data[8] % 8);
                        MyChar.Direction = Dir;

                        switch (Dir)
                        {
                            case 0:
                                {
                                    AddY = 1;
                                    break;
                                }
                            case 1:
                                {
                                    AddX = -1;
                                    AddY = 1;
                                    break;
                                }
                            case 2:
                                {
                                    AddX = -1;
                                    break;
                                }
                            case 3:
                                {
                                    AddX = -1;
                                    AddY = -1;
                                    break;
                                }
                            case 4:
                                {
                                    AddY = -1;
                                    break;
                                }
                            case 5:
                                {
                                    AddX = 1;
                                    AddY = -1;
                                    break;
                                }
                            case 6:
                                {
                                    AddX = 1;
                                    break;
                                }
                            case 7:
                                {
                                    AddY = 1;
                                    AddX = 1;
                                    break;
                                }
                        }
                        World.PlayerMoves(MyChar, Data);

                        MyChar.PrevX = MyChar.LocX;
                        MyChar.PrevY = MyChar.LocY;
                        MyChar.LocX = (ushort)(MyChar.LocX + AddX);
                        MyChar.LocY = (ushort)(MyChar.LocY + AddY);

                        MyChar.TargetUID = 0;
                        MyChar.MobTarget = null;
                        MyChar.PTarget = null;
                        MyChar.TGTarget = null;
                        MyChar.AtkType = 0;
                        MyChar.SkillLooping = 0;
                        MyChar.Action = 100;
                        World.SpawnMeToOthers(MyChar, true);
                        World.SpawnOthersToMe(MyChar, true);
                        World.SurroundNPCs(MyChar, true);
                        World.SurroundMobs(MyChar, true);
                        World.SurroundDroppedItems(MyChar, true);
                        MyChar.Attacking = false;
                        if (MyChar.Mining)
                            MyChar.Mining = false;

                        break;
                    }
                #endregion
                #region chat
                case 1004:
                    {
                        short ChatType = (short)(Data[8] | (Data[9] << 8));
                        int Pos = 26;
                        int Length = 0;
                        string From = "";
                        string To = "";
                        string Message = "";
                        for (int Count = 0; Count < Data[25]; Count += 1)
                        {
                            From += Convert.ToChar(Data[Pos]);
                            Pos += 1;
                        }

                        Length = Data[Pos];
                        Pos += 1;

                        for (int Count = 0; Count < Length; Count += 1)
                        {
                            To += Convert.ToChar(Data[Pos]);
                            Pos += 1;
                        }

                        Pos += 1;
                        Length = Data[Pos];
                        Pos += 1;

                        for (int Count = 0; Count < Length; Count += 1)
                        {
                            if (Pos <= Data.Length)
                                Message += Convert.ToChar(Data[Pos]);
                            Pos += 1;
                            #region commands
                        }
                        if (Message[0] == '@')
                        {
                            Commands.SendCommand(Message, MyChar, this);


                            string[] Splitter = Message.Split(' ');

                            /*
                            try
                            {
                                if (Message[0] == '/')
                                {

                                    #region playercommands
                                    if (Splitter[0] == "/dc")
                                    {
                                        Drop();
                                    }
                                    if (Splitter[0] == "/quest")
                                    {
                                        Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "You have to kill 300 " + MyChar.QuestMob + "s and you have killed " + MyChar.QuestKO + " so far.", 2000));
                                    }
                                    #endregion
                                    #region VIPCommands
                                    if (Status == 5)
                                    {
                                        if (Splitter[0] == "/vipmap")
                                        {
                                            MyChar.Teleport(1011, 188, 264);
                                        }
                                    }
                                    #endregion
                                    #region GMPMcommands
                                    if (Status == 8)
                                    {

                                        if (Splitter[0] == "/gwstart")
                                        {
                                            World.GWOn = true;
                                            World.SendMsgToAll("GuildWar has started!", "SYSTEM", 2011);
                                        }
                                        if (Splitter[0] == "/gwend")
                                        {
                                            World.GWOn = false;
                                            World.SendMsgToAll("GuildWar has ended!", "SYSTEM", 2011);
                                        }
                                        if (Splitter[0] == "/mm")
                                        {
                                            MyChar.Teleport(ushort.Parse(Splitter[1]), ushort.Parse(Splitter[2]), ushort.Parse(Splitter[3]));
                                        }
                                        if (Splitter[0] == "/setpkp")
                                        {
                                            ushort PKPoints = ushort.Parse(Splitter[1]);
                                            MyChar.PKPoints = PKPoints;
                                            World.UpdateSpawn(MyChar);
                                            Send(General.MyPackets.CharacterInfo(MyChar));
                                            MyChar.SendEquips(false);
                                            Send(General.MyPackets.Vital(MyChar.UID, 26, MyChar.GetStat()));

                                        }
                                        if (Splitter[0] == "/item2")
                                        {
                                            Ini ItemNames = new Ini(System.Windows.Forms.Application.StartupPath + @"\ItemNamesToId.ini");

                                            string ItemName = Splitter[2];
                                            string ItemQuality = Splitter[1];
                                            byte Plus = byte.Parse(Splitter[3]);
                                            byte Bless = byte.Parse(Splitter[4]);
                                            byte Enchant = byte.Parse(Splitter[5]);
                                            byte Soc1 = byte.Parse(Splitter[6]);
                                            byte Soc2 = byte.Parse(Splitter[7]);
                                            byte Position = byte.Parse(Splitter[8]);

                                            uint ItemId = 0;
                                            ItemId = uint.Parse(ItemNames.ReadString("Items", ItemName));

                                            if (ItemId == 0)
                                                return;

                                            byte Quality = 1;

                                            if (ItemQuality == "One")
                                                Quality = 1;
                                            else if (ItemQuality == "Normal")
                                                Quality = 5;
                                            else if (ItemQuality == "Unique")
                                                Quality = 7;
                                            else if (ItemQuality == "Refined")
                                                Quality = 6;
                                            else if (ItemQuality == "Elite")
                                                Quality = 8;
                                            else if (ItemQuality == "Super")
                                                Quality = 9;
                                            else
                                                Quality = (byte)Other.ItemQuality(ItemId);

                                            ItemId = Other.ItemQualityChange(ItemId, Quality);

                                            if (MyChar.ItemsInInventory < 40)
                                                MyChar.AddItem(ItemId.ToString() + "-" + Plus.ToString() + "-" + Bless.ToString() + "-" + Enchant.ToString() + "-" + Soc1.ToString() + "-" + Soc2.ToString(), Position, (uint)General.Rand.Next(57458353));

                                        }
                                        if (Splitter[0] == "/dc")
                                        {
                                            Drop();
                                        }
                                        if (Splitter[0] == "/vp")
                                        {
                                            uint NewVP = uint.Parse(Splitter[1]);

                                            MyChar.VP = NewVP;
                                        }
                                        if (Splitter[0] == "/refresh")
                                        {
                                            MyChar.Teleport(MyChar.LocMap, MyChar.LocX, MyChar.LocY);
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "area updated!!", 2005));
                                        }
                                        if (Splitter[0] == "/recall")
                                        {
                                            foreach (var DE in Kernel.Chars)
                                            {
                                                Character Char = DE.Value;
                                                if (Char.Name == Splitter[1])
                                                {
                                                    Char.Teleport(MyChar.LocMap, MyChar.LocX, MyChar.LocY);
                                                    break;
                                                }
                                                else
                                                {
                                                    Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "Sorry the Character:" + Splitter[1] + " is offline...Please try again later", 2000));
                                                }
                                            }

                                        }
                                        if (Splitter[0] == "/rev")
                                        {
                                            MyChar.CurHP = MyChar.MaxHP;
                                            MyChar.Alive = true;
                                            MyChar.MyClient.Send(General.MyPackets.Status1(MyChar.UID, 0));
                                            MyChar.MyClient.Send(General.MyPackets.Status3(MyChar.UID));
                                            MyChar.MyClient.Send(General.MyPackets.Vital(MyChar.UID, 26, MyChar.GetStat()));
                                            MyChar.MyClient.Send(General.MyPackets.CharacterInfo(MyChar));
                                            MyChar.SendEquips(false);
                                            MyChar.BlueName = false;
                                            MyChar.MyClient.Send(General.MyPackets.Vital(MyChar.UID, 26, MyChar.GetStat()));
                                            MyChar.Stamina = 100;
                                            MyChar.MyClient.Send(General.MyPackets.Vital(MyChar.UID, 9, MyChar.Stamina));
                                            MyChar.MyClient.Send(General.MyPackets.String(MyChar.UID, 10, "born3"));
                                            World.UpdateSpawn(MyChar);
                                        }
                                        if (Splitter[0] == "/sr")// restarts the server from ingame
                                        {
                                            World.SendMsgToAll("Server Restarting Please Log Off Immediately To Prevent Data Loss", "SYSTEM", 2011);
                                            Console.WriteLine("Server Restarting from In-Game");
                                            General.ServerRestart();
                                        }
                                        if (Splitter[0] == "/goto")
                                        {
                                            foreach (var DE in Kernel.Chars)
                                            {
                                                Character Char = DE.Value;
                                                if (Char.Name == Splitter[1])
                                                {
                                                    MyChar.Teleport(Char.LocMap, Char.LocX, Char.LocY);
                                                    break;
                                                }
                                                else
                                                {
                                                    Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "Sorry the Character:" + Splitter[1] + " is offline...Please try again later", 2000));
                                                }
                                            }
                                        }
                                        if (Splitter[0] == "/gmc")
                                        {
                                            Message = Message.Remove(0, 2);
                                            World.SendMsgToAll(Message, MyChar.Name, 2011);
                                        }
                                        if (Splitter[0] == "/xp")
                                        {
                                            MyChar.XpList = true;
                                            Send(General.MyPackets.Vital(MyChar.UID, 26, MyChar.GetStat()));
                                        }
                                        if (Splitter[0] == "/changename")
                                        {
                                            string newname = Splitter[1];
                                            MyChar.Name = newname;
                                            Send(General.MyPackets.CharacterInfo(MyChar));
                                            World.SpawnMeToOthers(MyChar, false);
                                            MyChar.SendEquips(false);
                                        }
                                        if (Splitter[0] == "/skill")
                                        {
                                            MyChar.LearnSkill2(short.Parse(Splitter[1]), byte.Parse(Splitter[2]));
                                        }
                                        if (Splitter[0] == "/model")
                                        {
                                            if (Splitter[1] == "smale")
                                                MyChar.Model = 1003;
                                            if (Splitter[1] == "lmale")
                                                MyChar.Model = 1004;
                                            if (Splitter[1] == "sfemale")
                                                MyChar.Model = 2001;
                                            if (Splitter[1] == "lfemale")
                                                MyChar.Model = 2002;

                                            Send(General.MyPackets.Vital(MyChar.UID, 12, ulong.Parse(MyChar.Avatar.ToString() + MyChar.Model.ToString())));
                                            World.UpdateSpawn(MyChar);
                                        }
                                        if (Splitter[0] == "/info")
                                        {
                                            string BackMsg = "";
                                            foreach (var DE in Kernel.Chars)
                                            {
                                                Character Char = DE.Value;
                                                BackMsg += Char.Name + ", ";
                                            }
                                            if (BackMsg.Length > 1)
                                                BackMsg = BackMsg.Remove(BackMsg.Length - 2, 2);
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "Players Online: " + Kernel.Chars.Count, 2000));
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, BackMsg, 2000));
                                        }
                                        if (Splitter[0] == "/scroll")
                                        {
                                            if (Splitter[1] == "pc")
                                                MyChar.Teleport(1011, 188, 264);
                                            if (Splitter[1] == "dc")
                                                MyChar.Teleport(1000, 500, 650);
                                            if (Splitter[1] == "bi")
                                                MyChar.Teleport(1015, 723, 573);
                                            if (Splitter[1] == "am")
                                                MyChar.Teleport(1020, 565, 562);
                                            if (Splitter[1] == "ma")
                                                MyChar.Teleport(1036, 198, 194);
                                            if (Splitter[1] == "tc")
                                                MyChar.Teleport(1002, 429, 378);
                                        }
                                        if (Splitter[0] == "/item")
                                        {
                                            Ini ItemNames = new Ini(System.Windows.Forms.Application.StartupPath + @"\ItemNamesToId.ini");
                                            string ItemName = Splitter[2];
                                            string ItemQuality = Splitter[1];
                                            byte Plus = byte.Parse(Splitter[3]);
                                            byte Bless = byte.Parse(Splitter[4]);
                                            byte Enchant = byte.Parse(Splitter[5]);
                                            byte Soc1 = byte.Parse(Splitter[6]);
                                            byte Soc2 = byte.Parse(Splitter[7]);

                                            uint ItemId = 0;
                                            ItemId = uint.Parse(ItemNames.ReadString("Items", ItemName));

                                            if (ItemId == 0)
                                                return;

                                            byte Quality = 1;

                                            if (ItemQuality == "One")
                                                Quality = 1;
                                            else if (ItemQuality == "Normal")
                                                Quality = 5;
                                            else if (ItemQuality == "Unique")
                                                Quality = 7;
                                            else if (ItemQuality == "Refined")
                                                Quality = 6;
                                            else if (ItemQuality == "Elite")
                                                Quality = 8;
                                            else if (ItemQuality == "Super")
                                                Quality = 9;
                                            else
                                                Quality = (byte)Other.ItemQuality(ItemId);

                                            ItemId = Other.ItemQualityChange(ItemId, Quality);

                                            if (MyChar.ItemsInInventory < 40)
                                                MyChar.AddItem(ItemId.ToString() + "-" + Plus.ToString() + "-" + Bless.ToString() + "-" + Enchant.ToString() + "-" + Soc1.ToString() + "-" + Soc2.ToString(), 0, (uint)General.Rand.Next(57458353));
                                        }
                                        if (Splitter[0] == "/clearpkp")
                                        {
                                            MyChar.PKPoints = 0;
                                            World.UpdateSpawn(MyChar);
                                            Send(General.MyPackets.CharacterInfo(MyChar));
                                            MyChar.SendEquips(false);
                                            Send(General.MyPackets.Vital(MyChar.UID, 26, MyChar.GetStat()));
                                        }
                                        if (Splitter[0] == "/pkp")
                                        {
                                            MyChar.PKPoints = 100;
                                            World.UpdateSpawn(MyChar);
                                            Send(General.MyPackets.CharacterInfo(MyChar));
                                            MyChar.SendEquips(false);
                                            Send(General.MyPackets.Vital(MyChar.UID, 26, MyChar.GetStat()));
                                        }
                                        if (Splitter[0] == "/kick")
                                        {
                                            foreach (var DE in Kernel.Chars)
                                            {
                                                Character Char = DE.Value;

                                                if (Char.Name == Splitter[1])
                                                {
                                                    World.SendMsgToAll(Splitter[1] + " has been kicked by " + MyChar.Name, "SYSTEM", 2011);
                                                    Char.MyClient.Drop();
                                                }
                                                else
                                                {
                                                    Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "Sorry the Character:" + Splitter[1] + " is offline...Please try again later", 2000));
                                                }
                                            }
                                        }
                                        if (Splitter[0] == "/ban")
                                        {
                                            foreach (var DE in Kernel.Chars)
                                            {
                                                Character Char = DE.Value;

                                                if (Char.Name == Splitter[1])
                                                {
                                                    World.SendMsgToAll(Splitter[1] + " has been banned by " + MyChar.Name, "SYSTEM", 2011);
                                                    DataBase.Ban(Char.MyClient.Account);
                                                    Char.MyClient.Drop();
                                                }
                                                else
                                                {
                                                    Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "Sorry the Character:" + Splitter[1] + " is offline...Please try again later", 2000));
                                                }
                                            }
                                        }
                                        if (Splitter[0] == "/effect")
                                        {
                                            Send(General.MyPackets.String(MyChar.UID, 10, Splitter[1]));
                                        }
                                        if (Splitter[0] == "/level")
                                        {
                                            byte NewLvl = byte.Parse(Splitter[1]);
                                            MyChar.Level = NewLvl;
                                            MyChar.Exp = 0;
                                            DataBase.GetStats(MyChar);
                                            MyChar.GetEquipStats(1, true);
                                            MyChar.GetEquipStats(2, true);
                                            MyChar.GetEquipStats(3, true);
                                            MyChar.GetEquipStats(4, true);
                                            MyChar.GetEquipStats(5, true);
                                            MyChar.GetEquipStats(6, true);
                                            MyChar.GetEquipStats(7, true);
                                            MyChar.GetEquipStats(8, true);
                                            MyChar.MinAtk = MyChar.Str;
                                            MyChar.MaxAtk = MyChar.Str;
                                            MyChar.MaxHP = MyChar.BaseMaxHP();
                                            MyChar.Potency = MyChar.Level;
                                            MyChar.GetEquipStats(1, false);
                                            MyChar.GetEquipStats(2, false);
                                            MyChar.GetEquipStats(3, false);
                                            MyChar.GetEquipStats(4, false);
                                            MyChar.GetEquipStats(5, false);
                                            MyChar.GetEquipStats(6, false);
                                            MyChar.GetEquipStats(7, false);
                                            MyChar.GetEquipStats(8, false);
                                            MyChar.CurHP = MyChar.MaxHP;
                                            Send(General.MyPackets.Vital((long)MyChar.UID, 13, MyChar.Level));
                                            Send(General.MyPackets.Vital((long)MyChar.UID, 16, MyChar.Str));
                                            Send(General.MyPackets.Vital((long)MyChar.UID, 17, MyChar.Agi));
                                            Send(General.MyPackets.Vital((long)MyChar.UID, 15, MyChar.Vit));
                                            Send(General.MyPackets.Vital((long)MyChar.UID, 14, MyChar.Wis));
                                            Send(General.MyPackets.Vital((long)MyChar.UID, 5, MyChar.Exp));
                                            Send(General.MyPackets.Vital((long)MyChar.UID, 2, MyChar.MaxCurMP()));
                                            Send(General.MyPackets.GeneralData((long)MyChar.UID, 0, 0, 0, 92));
                                            if (MyChar.MyGuild != null)
                                                MyChar.MyGuild.Refresh(MyChar);
                                        }
                                        if (Splitter[0] == "/job")
                                        {
                                            byte NewJob = byte.Parse(Splitter[1]);
                                            MyChar.Job = NewJob;
                                            DataBase.GetStats(MyChar);
                                            MyChar.GetEquipStats(1, true);
                                            MyChar.GetEquipStats(2, true);
                                            MyChar.GetEquipStats(3, true);
                                            MyChar.GetEquipStats(4, true);
                                            MyChar.GetEquipStats(5, true);
                                            MyChar.GetEquipStats(6, true);
                                            MyChar.GetEquipStats(7, true);
                                            MyChar.GetEquipStats(8, true);
                                            MyChar.MinAtk = MyChar.Str;
                                            MyChar.MaxAtk = MyChar.Str;
                                            MyChar.MaxHP = MyChar.BaseMaxHP();
                                            MyChar.Potency = MyChar.Level;
                                            MyChar.GetEquipStats(1, false);
                                            MyChar.GetEquipStats(2, false);
                                            MyChar.GetEquipStats(3, false);
                                            MyChar.GetEquipStats(4, false);
                                            MyChar.GetEquipStats(5, false);
                                            MyChar.GetEquipStats(6, false);
                                            MyChar.GetEquipStats(7, false);
                                            MyChar.GetEquipStats(8, false);
                                            MyChar.CurHP = MyChar.MaxHP;
                                            Send(General.MyPackets.Vital((long)MyChar.UID, 7, MyChar.Job));
                                            Send(General.MyPackets.Vital((long)MyChar.UID, 16, MyChar.Str));
                                            Send(General.MyPackets.Vital((long)MyChar.UID, 17, MyChar.Agi));
                                            Send(General.MyPackets.Vital((long)MyChar.UID, 15, MyChar.Vit));
                                            Send(General.MyPackets.Vital((long)MyChar.UID, 14, MyChar.Wis));
                                            Send(General.MyPackets.Vital((long)MyChar.UID, 2, MyChar.MaxCurMP()));
                                            Send(General.MyPackets.GeneralData((long)MyChar.UID, 0, 0, 0, 92));
                                        }
                                        if (Splitter[0] == "/prof")
                                        {
                                            if (MyChar.Profs.Contains(short.Parse(Splitter[1])))
                                                MyChar.Profs.Remove(short.Parse(Splitter[1]));

                                            if (MyChar.Prof_Exps.Contains(short.Parse(Splitter[1])))
                                                MyChar.Prof_Exps.Remove(short.Parse(Splitter[1]));

                                            MyChar.Profs.Add(short.Parse(Splitter[1]), byte.Parse(Splitter[2]));
                                            MyChar.Prof_Exps.Add(short.Parse(Splitter[1]), uint.Parse("0"));
                                            Send(General.MyPackets.Prof(short.Parse(Splitter[1]), byte.Parse(Splitter[2]), 0));
                                        }
                                        if (Splitter[0] == "/gold")
                                        {
                                            uint NewSilvers = uint.Parse(Splitter[1]);

                                            MyChar.Silvers = NewSilvers;
                                        }
                                        if (Splitter[0] == "/cps")
                                        {
                                            uint NewCPs = uint.Parse(Splitter[1]);

                                            MyChar.CPs = NewCPs;
                                        }
                                        if (Splitter[0] == "/gm")
                                        {
                                            if (Splitter[1] == "tro")
                                            {
                                                MyChar.AddItem("420339-12-7-255-13-13", 0, (uint)General.Rand.Next(57458353));
                                                MyChar.AddItem("410339-12-7-255-13-13", 0, (uint)General.Rand.Next(57458353));
                                                MyChar.AddItem("480339-12-7-255-13-13", 0, (uint)General.Rand.Next(57458353));
                                                MyChar.AddItem("160249-12-7-255-13-13", 0, (uint)General.Rand.Next(57458353));//blizzard
                                                MyChar.AddItem("120249-12-7-255-13-13", 0, (uint)General.Rand.Next(57458353));//tornado
                                                MyChar.AddItem("150249-12-7-255-13-13", 0, (uint)General.Rand.Next(57458353));//thunder
                                                MyChar.AddItem("112389-12-7-255-13-13", 0, (uint)General.Rand.Next(57458353));
                                                MyChar.AddItem("135299-12-7-255-13-13", 0, (uint)General.Rand.Next(57458353));
                                            }
                                            if (Splitter[1] == "tao")
                                            {
                                                MyChar.AddItem("139299-12-7-255-3-3", 0, (uint)General.Rand.Next(57458353));
                                                MyChar.AddItem("112349-12-7-255-3-3", 0, (uint)General.Rand.Next(57458353));
                                                MyChar.AddItem("121249-12-7-255-3-3", 0, (uint)General.Rand.Next(57458353));
                                                MyChar.AddItem("152259-12-7-255-3-3", 0, (uint)General.Rand.Next(57458353));
                                                MyChar.AddItem("160249-12-7-255-3-3", 0, (uint)General.Rand.Next(57458353));//blizzard
                                                MyChar.AddItem("421339-12-7-255-3-3", 0, (uint)General.Rand.Next(57458353));
                                            }
                                            if (Splitter[1] == "arch")
                                            {
                                                MyChar.AddItem("138299-12-7-255-13-13", 0, (uint)General.Rand.Next(57458353));
                                                MyChar.AddItem("160249-12-7-255-13-13", 0, (uint)General.Rand.Next(57458353));//blizzard
                                                MyChar.AddItem("120249-12-7-255-13-13", 0, (uint)General.Rand.Next(57458353));//tornado
                                                MyChar.AddItem("150249-12-7-255-13-13", 0, (uint)General.Rand.Next(57458353));//thunder
                                                MyChar.AddItem("112339-12-7-255-13-13", 0, (uint)General.Rand.Next(57458353));
                                                MyChar.AddItem("500329-12-7-255-13-13", 0, (uint)General.Rand.Next(57458353));
                                                MyChar.AddItem("1050002-12-7-255-13-13", 0, (uint)General.Rand.Next(57458353));

                                            }
                                            if (Splitter[1] == "war")
                                            {
                                                MyChar.AddItem("480339-12-7-255-13-13", 0, (uint)General.Rand.Next(57458353));
                                                MyChar.AddItem("136299-12-7-255-13-13", 0, (uint)General.Rand.Next(57458353));
                                                MyChar.AddItem("160249-12-7-255-13-13", 0, (uint)General.Rand.Next(57458353));//blizzard
                                                MyChar.AddItem("120249-12-7-255-13-13", 0, (uint)General.Rand.Next(57458353));//tornado
                                                MyChar.AddItem("150249-12-7-255-13-13", 0, (uint)General.Rand.Next(57458353));//thunder
                                                MyChar.AddItem("112419-12-7-255-13-13", 0, (uint)General.Rand.Next(57458353));
                                                MyChar.AddItem("560339-12-7-255-13-13", 0, (uint)General.Rand.Next(57458353));
                                                MyChar.AddItem("561339-12-7-255-13-13", 0, (uint)General.Rand.Next(57458353));
                                                MyChar.AddItem("900399-12-7-255-13-13", 0, (uint)General.Rand.Next(57458353));
                                            }
                                        }
                                        if (Splitter[0] == "/killmap")
                                        {
                                            foreach (ushort[] revp in DataBase.RevPoints)
                                                foreach (var DE in Kernel.Chars)
                                                {
                                                    Character Char = DE.Value;

                                                    int EModel;
                                                    if (Char.Model == 1003 || Char.Model == 1004)
                                                        EModel = 15099;
                                                    else
                                                        EModel = 15199;

                                                    if (revp[0] == Char.LocMap)
                                                    {
                                                        Char.Die();
                                                        Char.CurHP = 0;
                                                        Char.Death = DateTime.Now;
                                                        Char.Alive = false;
                                                        Char.XpList = false;
                                                        Char.SMOn = false;
                                                        Char.CycloneOn = false;
                                                        Char.XpCircle = 0;
                                                        Char.Flying = false;
                                                        Char.MyClient.Send(General.MyPackets.Vital(Char.UID, 26, Char.GetStat()));
                                                        Char.MyClient.Send(General.MyPackets.Status1(Char.UID, EModel));
                                                        Char.MyClient.Send(General.MyPackets.Death(Char));
                                                        Char.MyClient.Send(General.MyPackets.SendMsg(MyChar.MyClient.UID, "SYSTEM", MyChar.Name, "Map ID " + revp[0] + " Has just been killed", 2011));
                                                        World.UpdateSpawn(Char);
                                                    }
                                                }
                                        }

                                        // Shutdown Commands
                                    }
                                    #endregion
                                    #region PMcommands
                                    if (Status == 7)
                                    {
                                        if (Splitter[0] == "/refresh")
                                        {
                                            MyChar.Teleport(MyChar.LocMap, MyChar.LocX, MyChar.LocY);
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "area updated!!", 2005));
                                        }
                                        if (Splitter[0] == "/mm")
                                        {
                                            MyChar.Teleport(ushort.Parse(Splitter[1]), ushort.Parse(Splitter[2]), ushort.Parse(Splitter[3]));
                                        }
                                        if (Splitter[0] == "/setpkp")
                                        {
                                            ushort PKPoints = ushort.Parse(Splitter[1]);
                                            MyChar.PKPoints = PKPoints;
                                            World.UpdateSpawn(MyChar);
                                            Send(General.MyPackets.CharacterInfo(MyChar));
                                            MyChar.SendEquips(false);
                                            Send(General.MyPackets.Vital(MyChar.UID, 26, MyChar.GetStat()));

                                        }
                                        if (Splitter[0] == "/kick")
                                        {
                                            foreach (var DE in Kernel.Chars)
                                            {
                                                Character Char = DE.Value;

                                                if (Char.Name == Splitter[1])
                                                {
                                                    World.SendMsgToAll(Splitter[1] + " has been kicked by " + MyChar.Name, "SYSTEM", 2011);
                                                    Char.MyClient.Drop();
                                                }
                                            }
                                        }
                                        if (Splitter[0] == "/item")
                                        {
                                            Ini ItemNames = new Ini(System.Windows.Forms.Application.StartupPath + @"\ItemNamesToId.ini");
                                            string ItemName = Splitter[2];
                                            string ItemQuality = Splitter[1];
                                            byte Plus = byte.Parse(Splitter[3]);
                                            byte Bless = byte.Parse(Splitter[4]);
                                            byte Enchant = byte.Parse(Splitter[5]);
                                            byte Soc1 = byte.Parse(Splitter[6]);
                                            byte Soc2 = byte.Parse(Splitter[7]);

                                            uint ItemId = 0;
                                            ItemId = uint.Parse(ItemNames.ReadString("Items", ItemName));

                                            if (ItemId == 0)
                                                return;

                                            byte Quality = 1;

                                            if (ItemQuality == "One")
                                                Quality = 1;
                                            else if (ItemQuality == "Normal")
                                                Quality = 5;
                                            else if (ItemQuality == "Unique")
                                                Quality = 7;
                                            else if (ItemQuality == "Refined")
                                                Quality = 6;
                                            else if (ItemQuality == "Elite")
                                                Quality = 8;
                                            else if (ItemQuality == "Super")
                                                Quality = 9;
                                            else
                                                Quality = (byte)Other.ItemQuality(ItemId);

                                            ItemId = Other.ItemQualityChange(ItemId, Quality);

                                            if (MyChar.ItemsInInventory < 40)
                                                MyChar.AddItem(ItemId.ToString() + "-" + Plus.ToString() + "-" + Bless.ToString() + "-" + Enchant.ToString() + "-" + Soc1.ToString() + "-" + Soc2.ToString(), 0, (uint)General.Rand.Next(57458353));
                                        }
                                        if (Splitter[0] == "/sr")// restarts the server from ingame
                                        {
                                            World.SendMsgToAll("Server Restarting!", "SYSTEM", 2011);
                                            Console.WriteLine("Server Restarting from In-Game");
                                            General.ServerRestart();
                                        }
                                        if (Splitter[0] == "/killmap")
                                        {
                                            foreach (ushort[] revp in DataBase.RevPoints)
                                                foreach (var DE in Kernel.Chars)
                                                {
                                                    Character Char = DE.Value;

                                                    int EModel;
                                                    if (Char.Model == 1003 || Char.Model == 1004)
                                                        EModel = 15099;
                                                    else
                                                        EModel = 15199;

                                                    if (revp[0] == Char.LocMap)
                                                    {
                                                        Char.Die();
                                                        Char.CurHP = 0;
                                                        Char.Death = DateTime.Now;
                                                        Char.Alive = false;
                                                        Char.XpList = false;
                                                        Char.SMOn = false;
                                                        Char.CycloneOn = false;
                                                        Char.XpCircle = 0;
                                                        Char.Flying = false;
                                                        Char.MyClient.Send(General.MyPackets.Vital(Char.UID, 26, Char.GetStat()));
                                                        Char.MyClient.Send(General.MyPackets.Status1(Char.UID, EModel));
                                                        Char.MyClient.Send(General.MyPackets.Death(Char));
                                                        Char.MyClient.Send(General.MyPackets.SendMsg(MyChar.MyClient.UID, "SYSTEM", MyChar.Name, "Map ID " + revp[0] + " Has just been killed", 2011));
                                                        World.UpdateSpawn(Char);
                                                    }
                                                }
                                        }
                                        if (Splitter[0] == "/effect")
                                        {
                                            Send(General.MyPackets.String(MyChar.UID, 10, Splitter[1]));
                                        }
                                        if (Splitter[0] == "/xp")
                                        {
                                            MyChar.XpList = true;
                                            Send(General.MyPackets.Vital(MyChar.UID, 26, MyChar.GetStat()));
                                        }
                                        if (Splitter[0] == "/ban")
                                        {
                                            foreach (var DE in Kernel.Chars)
                                            {
                                                Character Char = DE.Value;

                                                if (Char.Name == Splitter[1])
                                                {
                                                    World.SendMsgToAll(Splitter[1] + " has been banned by " + MyChar.Name, "SYSTEM", 2011);
                                                    DataBase.Ban(Char.MyClient.Account);
                                                    Char.MyClient.Drop();
                                                }
                                                else
                                                {
                                                    Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "Sorry the Character:" + Splitter[1] + " is offline...Please try again later", 2000));
                                                }
                                            }
                                        }
                                        if (Splitter[0] == "/clearpkp")
                                        {
                                            MyChar.PKPoints = 0;
                                            World.UpdateSpawn(MyChar);
                                            Send(General.MyPackets.CharacterInfo(MyChar));
                                            MyChar.SendEquips(false);
                                            Send(General.MyPackets.Vital(MyChar.UID, 26, MyChar.GetStat()));
                                        }
                                        if (Splitter[0] == "/pkp")
                                        {
                                            MyChar.PKPoints = 100;
                                            World.UpdateSpawn(MyChar);
                                            Send(General.MyPackets.CharacterInfo(MyChar));
                                            MyChar.SendEquips(false);
                                            Send(General.MyPackets.Vital(MyChar.UID, 26, MyChar.GetStat()));
                                        }
                                        if (Splitter[0] == "/level")
                                        {
                                            byte NewLvl = byte.Parse(Splitter[1]);
                                            MyChar.Level = NewLvl;
                                            MyChar.Exp = 0;
                                            DataBase.GetStats(MyChar);
                                            MyChar.GetEquipStats(1, true);
                                            MyChar.GetEquipStats(2, true);
                                            MyChar.GetEquipStats(3, true);
                                            MyChar.GetEquipStats(4, true);
                                            MyChar.GetEquipStats(5, true);
                                            MyChar.GetEquipStats(6, true);
                                            MyChar.GetEquipStats(7, true);
                                            MyChar.GetEquipStats(8, true);
                                            MyChar.MinAtk = MyChar.Str;
                                            MyChar.MaxAtk = MyChar.Str;
                                            MyChar.MaxHP = MyChar.BaseMaxHP();
                                            MyChar.Potency = MyChar.Level;
                                            MyChar.GetEquipStats(1, false);
                                            MyChar.GetEquipStats(2, false);
                                            MyChar.GetEquipStats(3, false);
                                            MyChar.GetEquipStats(4, false);
                                            MyChar.GetEquipStats(5, false);
                                            MyChar.GetEquipStats(6, false);
                                            MyChar.GetEquipStats(7, false);
                                            MyChar.GetEquipStats(8, false);
                                            MyChar.CurHP = MyChar.MaxHP;
                                            Send(General.MyPackets.Vital((long)MyChar.UID, 13, MyChar.Level));
                                            Send(General.MyPackets.Vital((long)MyChar.UID, 16, MyChar.Str));
                                            Send(General.MyPackets.Vital((long)MyChar.UID, 17, MyChar.Agi));
                                            Send(General.MyPackets.Vital((long)MyChar.UID, 15, MyChar.Vit));
                                            Send(General.MyPackets.Vital((long)MyChar.UID, 14, MyChar.Wis));
                                            Send(General.MyPackets.Vital((long)MyChar.UID, 5, MyChar.Exp));
                                            Send(General.MyPackets.Vital((long)MyChar.UID, 2, MyChar.MaxCurMP()));
                                            Send(General.MyPackets.GeneralData((long)MyChar.UID, 0, 0, 0, 92));
                                            if (MyChar.MyGuild != null)
                                                MyChar.MyGuild.Refresh(MyChar);
                                        }
                                        if (Splitter[0] == "/skill")
                                        {
                                            MyChar.LearnSkill2(short.Parse(Splitter[1]), byte.Parse(Splitter[2]));
                                        }
                                        if (Splitter[0] == "/job")
                                        {
                                            byte NewJob = byte.Parse(Splitter[1]);
                                            MyChar.Job = NewJob;
                                            DataBase.GetStats(MyChar);
                                            MyChar.GetEquipStats(1, true);
                                            MyChar.GetEquipStats(2, true);
                                            MyChar.GetEquipStats(3, true);
                                            MyChar.GetEquipStats(4, true);
                                            MyChar.GetEquipStats(5, true);
                                            MyChar.GetEquipStats(6, true);
                                            MyChar.GetEquipStats(7, true);
                                            MyChar.GetEquipStats(8, true);
                                            MyChar.MinAtk = MyChar.Str;
                                            MyChar.MaxAtk = MyChar.Str;
                                            MyChar.MaxHP = MyChar.BaseMaxHP();
                                            MyChar.Potency = MyChar.Level;
                                            MyChar.GetEquipStats(1, false);
                                            MyChar.GetEquipStats(2, false);
                                            MyChar.GetEquipStats(3, false);
                                            MyChar.GetEquipStats(4, false);
                                            MyChar.GetEquipStats(5, false);
                                            MyChar.GetEquipStats(6, false);
                                            MyChar.GetEquipStats(7, false);
                                            MyChar.GetEquipStats(8, false);
                                            MyChar.CurHP = MyChar.MaxHP;
                                            Send(General.MyPackets.Vital((long)MyChar.UID, 7, MyChar.Job));
                                            Send(General.MyPackets.Vital((long)MyChar.UID, 16, MyChar.Str));
                                            Send(General.MyPackets.Vital((long)MyChar.UID, 17, MyChar.Agi));
                                            Send(General.MyPackets.Vital((long)MyChar.UID, 15, MyChar.Vit));
                                            Send(General.MyPackets.Vital((long)MyChar.UID, 14, MyChar.Wis));
                                            Send(General.MyPackets.Vital((long)MyChar.UID, 2, MyChar.MaxCurMP()));
                                            Send(General.MyPackets.GeneralData((long)MyChar.UID, 0, 0, 0, 92));
                                        }
                                        if (Splitter[0] == "/prof")
                                        {
                                            if (MyChar.Profs.Contains(short.Parse(Splitter[1])))
                                                MyChar.Profs.Remove(short.Parse(Splitter[1]));

                                            if (MyChar.Prof_Exps.Contains(short.Parse(Splitter[1])))
                                                MyChar.Prof_Exps.Remove(short.Parse(Splitter[1]));

                                            MyChar.Profs.Add(short.Parse(Splitter[1]), byte.Parse(Splitter[2]));
                                            MyChar.Prof_Exps.Add(short.Parse(Splitter[1]), uint.Parse("0"));
                                            Send(General.MyPackets.Prof(short.Parse(Splitter[1]), byte.Parse(Splitter[2]), 0));
                                        }
                                        if (Splitter[0] == "/gold")
                                        {
                                            uint NewSilvers = uint.Parse(Splitter[1]);

                                            MyChar.Silvers = NewSilvers;
                                        }
                                        if (Splitter[0] == "/cps")
                                        {
                                            uint NewCPs = uint.Parse(Splitter[1]);

                                            MyChar.CPs = NewCPs;
                                        }
                                        if (Splitter[0] == "/gmc")
                                        {
                                            Message = Message.Remove(0, 2);
                                            World.SendMsgToAll(Message, MyChar.Name, 2011);
                                        }
                                        if (Splitter[0] == "/info")
                                        {
                                            string BackMsg = "";
                                            foreach (var DE in Kernel.Chars)
                                            {
                                                Character Char = DE.Value;
                                                BackMsg += Char.Name + ", ";
                                            }
                                            if (BackMsg.Length > 1)
                                                BackMsg = BackMsg.Remove(BackMsg.Length - 2, 2);
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "Players Online: " + Kernel.Chars.Count, 2000));
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, BackMsg, 2000));
                                        }
                                        if (Splitter[0] == "/changename")
                                        {
                                            string newname = Splitter[1];
                                            MyChar.Name = newname;
                                            Send(General.MyPackets.CharacterInfo(MyChar));
                                            World.SpawnMeToOthers(MyChar, false);
                                            MyChar.SendEquips(false);
                                        }
                                        if (Splitter[0] == "/recall")
                                        {
                                            foreach (var DE in Kernel.Chars)
                                            {
                                                Character Char = DE.Value;
                                                if (Char.Name == Splitter[1])
                                                {
                                                    Char.Teleport(MyChar.LocMap, MyChar.LocX, MyChar.LocY);
                                                    break;
                                                }
                                            }
                                        }
                                        if (Splitter[0] == "/goto")
                                        {
                                            foreach (var DE in Kernel.Chars)
                                            {
                                                Character Char = DE.Value;
                                                if (Char.Name == Splitter[1])
                                                {
                                                    MyChar.Teleport(Char.LocMap, Char.LocX, Char.LocY);
                                                    break;
                                                }
                                            }
                                        }
                                        if (Splitter[0] == "/gwstart")
                                        {
                                            World.GWOn = true;
                                            World.SendMsgToAll("GuildWar has started!", "SYSTEM", 2011);
                                        }
                                        if (Splitter[0] == "gwend")
                                        {
                                            World.GWOn = false;
                                            World.SendMsgToAll("GuildWar has ended!", "SYSTEM", 2011);
                                        }
                                        if (Splitter[0] == "/dc")
                                        {
                                            Drop();
                                        }
                                        if (Splitter[0] == "/scroll")
                                        {
                                            if (Splitter[1] == "pc")
                                                MyChar.Teleport(1011, 188, 264);
                                            if (Splitter[1] == "dc")
                                                MyChar.Teleport(1000, 500, 650);
                                            if (Splitter[1] == "bi")
                                                MyChar.Teleport(1015, 723, 573);
                                            if (Splitter[1] == "am")
                                                MyChar.Teleport(1020, 565, 562);
                                            if (Splitter[1] == "ma")
                                                MyChar.Teleport(1036, 198, 194);
                                            if (Splitter[1] == "tc")
                                                MyChar.Teleport(1002, 429, 378);
                                        }
                                    }
                                    #endregion
                                    #region phhelpcommands
                                    if (Status == 7)
                                    {
                                        if (Splitter[0] == "/help")
                                        {
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "The List of Commands", 2000));
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "For traveling commands type /travel", 2000));
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "For controlling other players type /control", 2000));
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "For personal commands type /personal", 2000));
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "For misc. commands type /other", 2000));
                                        }
                                        if (Splitter[0] == "/travel")
                                        {
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "Traveling Commands", 2000));
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "/scroll", 2000));
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "/recall", 2000));
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "/goto", 2000));
                                        }
                                        if (Splitter[0] == "/control")
                                        {
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "Control Commands", 2000));
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "/kick", 2000));
                                        }
                                        if (Splitter[0] == "/personal")
                                        {
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "Personal Commands", 2000));
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "/level", 2000));
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "/item", 2000));
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "/silvers", 2000));
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "/cps", 2000));
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "/changename", 2000));
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "/job", 2000));
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "/skill", 2000));

                                        }
                                        if (Splitter[0] == "/other")
                                        {
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "Other Commands", 2000));
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "/c", 2000));
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "/info", 2000));
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "/dc", 2000));
                                        }
                                    }
                                    #endregion
                                    #region gmhelpcommands
                                    if (Status == 8)
                                    {
                                        if (Splitter[0] == "/help")
                                        {
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "The List of Commands", 2000));
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "For traveling commands type /travel", 2000));
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "For controlling other players type /control", 2000));
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "For personal commands type /personal", 2000));
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "For personal commands type /server", 2000));
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "For misc. commands type /other", 2000));
                                        }
                                        if (Splitter[0] == "/travel")
                                        {
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "Traveling Commands", 2000));
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "/scroll", 2000));
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "/recall", 2000));
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "/goto", 2000));
                                        }
                                        if (Splitter[0] == "/control")
                                        {
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "Control Commands", 2000));
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "/kick", 2000));
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "/ban", 2000));
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "/ipban", 2000));
                                        }
                                        if (Splitter[0] == "/personal")
                                        {
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "Personal Commands", 2000));
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "/level", 2000));
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "/silvers", 2000));
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "/cps", 2000));
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "/changename", 2000));
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "/job", 2000));
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "/item", 2000));
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "/kill", 2000));
                                        }
                                        if (Splitter[0] == "/other")
                                        {
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "Other Commands", 2000));
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "/c", 2000));
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "/info", 2000));
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "/dc", 2000));
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "/killmap", 2000));
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "/gwstart", 2000));
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "/gwstop", 2000));
                                        }
                                        if (Splitter[0] == "/server")
                                        {
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "Server Commands", 2000));
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "/shutdownserver", 2000));
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "/stopshutdown", 2000));
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "/updaterestart", 2000));
                                            Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "/stoprestart", 2000));
                                        }
                                    }

                                }
                                    #endregion
                            }

                            catch { }
                             * */

                        }
                            #endregion
                        else
                        {
                            World.Chat(MyChar, ChatType, Data, To, Message);
                        }
                        if (ChatType == 2111)
                            if (MyChar.MyGuild != null)
                            {
                                MyChar.MyGuild.Bulletin = Message;
                                Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, MyChar.MyGuild.Bulletin, 2111));
                            }
                        break;
                    }
                #endregion
                #region whmoney
                case 1009:
                    {
                        PacketType = Data[0x0c];
                        switch (PacketType)
                        {
                            case 9:
                                {
                                    uint NPCID = BitConverter.ToUInt32(Data, 4); //(uint)((Data[7] << 24) + (Data[6] << 16) + (Data[5] << 8) + (Data[4]));

                                    byte WH = 0;

                                    if (NPCID == 8)
                                        WH = 0;
                                    else if (NPCID == 81)
                                        WH = 1;
                                    else if (NPCID == 82)
                                        WH = 2;
                                    else if (NPCID == 83)
                                        WH = 3;
                                    else if (NPCID == 84)
                                        WH = 4;
                                    else if (NPCID == 85)
                                        WH = 5;

                                    Send(General.MyPackets.OpenWarehouse(NPCID, MyChar.WHSilvers));
                                    Send(General.MyPackets.WhItems(MyChar, WH, (ushort)NPCID));
                                    break;
                                }
                            case 10://Deposit
                                {
                                    uint Amount = BitConverter.ToUInt32(Data, 8);//(uint)((Data[11] << 24) + (Data[10] << 16) + (Data[9] << 8) + (Data[8]));

                                    if (MyChar.Silvers >= Amount)
                                    {
                                        MyChar.Silvers -= Amount;
                                        MyChar.WHSilvers += Amount;
                                        Send(General.MyPackets.Vital(MyChar.UID, 10, MyChar.WHSilvers));
                                    }
                                    break;
                                }
                            case 11://Withdraw
                                {
                                    uint Amount = BitConverter.ToUInt32(Data, 8);// (uint)((Data[11] << 24) + (Data[10] << 16) + (Data[9] << 8) + (Data[8]));

                                    if (MyChar.WHSilvers >= Amount)
                                    {
                                        MyChar.Silvers += Amount;
                                        MyChar.WHSilvers -= Amount;
                                        Send(General.MyPackets.Vital(MyChar.UID, 10, MyChar.WHSilvers));
                                    }
                                    break;
                                }
                #endregion
                #region itemupgrade
                case 20:
                    {
                        
                        uint UppedItemUID = BitConverter.ToUInt32(Data, 4);//(uint)((Data[7] << 24) + (Data[6] << 16) + (Data[5] << 8) + Data[4]);
                        uint UppingItemUID = BitConverter.ToUInt32(Data, 8);//(uint)((Data[11] << 24) + (Data[10] << 16) + (Data[9] << 8) + Data[8]);

                        string UppedItem = "";
                        string UppingItem = "";
                        int Counter = 0;

                        foreach (uint uid in MyChar.Inventory_UIDs)
                        {
                            if (uid == UppedItemUID)
                                UppedItem = MyChar.Inventory[Counter];

                            Counter++;
                        }

                        Counter = 0;

                        foreach (uint uid in MyChar.Inventory_UIDs)
                        {
                            if (uid == UppingItemUID)
                                UppingItem = MyChar.Inventory[Counter];

                            Counter++;
                        }

                        string[] Splitter = UppedItem.Split('-');
                        uint UppedItem2 = uint.Parse(Splitter[0]);
                        string[] Splitter2 = UppingItem.Split('-');
                        uint UppingItem2 = uint.Parse(Splitter2[0]);

                        if (UppingItem2 == 1088001)
                            if (Other.Upgradable(UppedItem2))
                                if (Other.ItemInfo(UppedItem2)[3] < 130)
                                    if (Other.ItemType2(UppedItem2) == 11 && Other.WeaponType(UppedItem2) != 117 && Other.ItemInfo(UppedItem2)[3] < 120 || Other.WeaponType(UppedItem2) == 117 && Other.ItemInfo(UppedItem2)[3] < 112 || Other.ItemType2(UppedItem2) == 13 && Other.ItemInfo(UppedItem2)[3] < 120 || Other.ItemType2(UppedItem2) == 15 && Other.ItemInfo(UppedItem2)[3] < 127 || Other.ItemType2(UppedItem2) == 16 && Other.ItemInfo(UppedItem2)[3] < 129 || Other.ItemType(UppedItem2) == 4 || Other.ItemType(UppedItem2) == 5 || Other.ItemType2(UppedItem2) == 12 || Other.WeaponType(UppedItem2) == 132 && Other.ItemInfo(UppedItem2)[3] <= 12)
                                    {
                                        bool Success = false;
                                        double RemoveChance = 0;

                                        RemoveChance = Other.ItemInfo(UppedItem2)[3] / 3;

                                        if (Other.ItemQuality(UppedItem2) == 3 || Other.ItemQuality(UppedItem2) == 4 || Other.ItemQuality(UppedItem2) == 5)
                                            if (Other.ChanceSuccess(90 - RemoveChance))
                                                Success = true;

                                        if (Other.ItemQuality(UppedItem2) == 6)
                                            if (Other.ChanceSuccess(75 - RemoveChance))
                                                Success = true;

                                        if (Other.ItemQuality(UppedItem2) == 7)
                                            if (Other.ChanceSuccess(60 - RemoveChance))
                                                Success = true;

                                        if (Other.ItemQuality(UppedItem2) == 8)
                                            if (Other.ChanceSuccess(50 - RemoveChance))
                                                Success = true;

                                        if (Other.ItemQuality(UppedItem2) == 9)
                                            if (Other.ChanceSuccess(45 - RemoveChance))
                                                Success = true;

                                        if (Success)
                                        {
                                            MyChar.RemoveItem((ulong)UppedItemUID);

                                            UppedItem2 = Other.EquipNextLevel(UppedItem2);

                                            if (Splitter[4] == "0")
                                                if (Other.ChanceSuccess(0.5))
                                                    Splitter[4] = "255";

                                            if (Splitter[5] == "0")
                                                if (Splitter[4] != "0")
                                                    if (Other.ChanceSuccess(0.25))
                                                        Splitter[5] = "255";

                                            MyChar.AddItem(Convert.ToString(UppedItem2) + "-" + Splitter[1] + "-" + Splitter[2] + "-" + Splitter[3] + "-" + Splitter[4] + "-" + Splitter[5], 0, UppedItemUID);
                                        }

                                        MyChar.RemoveItem((ulong)UppingItemUID);
                                    }
                        
                        break;
                    }
                case 19:
                    {

                        uint UppedItemUID = BitConverter.ToUInt32(Data, 4);//(uint)((Data[7] << 24) + (Data[6] << 16) + (Data[5] << 8) + Data[4]);
                        uint UppingItemUID = BitConverter.ToUInt32(Data, 8);//(uint)((Data[11] << 24) + (Data[10] << 16) + (Data[9] << 8) + Data[8]);

                        string UppedItem = "";
                        string UppingItem = "";
                        int Counter = 0;

                        foreach (uint uid in MyChar.Inventory_UIDs)
                        {
                            if (uid == UppedItemUID)
                                UppedItem = MyChar.Inventory[Counter];

                            Counter++;
                        }

                        Counter = 0;

                        foreach (uint uid in MyChar.Inventory_UIDs)
                        {
                            if (uid == UppingItemUID)
                                UppingItem = MyChar.Inventory[Counter];

                            Counter++;
                        }

                        string[] Splitter = UppedItem.Split('-');
                        uint UppedItem2 = uint.Parse(Splitter[0]);
                        string[] Splitter2 = UppingItem.Split('-');
                        uint UppingItem2 = uint.Parse(Splitter2[0]);

                        if (UppingItem2 == 1088000)
                            if (Other.Upgradable(UppedItem2))
                                if (Other.ItemQuality(UppedItem2) != 9)
                                {
                                    bool Success = false;
                                    double RemoveChance = 0;

                                    RemoveChance = Other.ItemInfo(UppedItem2)[3] / 3;

                                    if (Other.ItemQuality(UppedItem2) == 3 || Other.ItemQuality(UppedItem2) == 4 || Other.ItemQuality(UppedItem2) == 5)
                                        if (Other.ChanceSuccess(64 - RemoveChance))
                                            Success = true;

                                    if (Other.ItemQuality(UppedItem2) == 6)
                                        if (Other.ChanceSuccess(54 - RemoveChance))
                                            Success = true;

                                    if (Other.ItemQuality(UppedItem2) == 7)
                                        if (Other.ChanceSuccess(48 - RemoveChance))
                                            Success = true;

                                    if (Other.ItemQuality(UppedItem2) == 8)
                                        if (Other.ChanceSuccess(44 - RemoveChance))
                                            Success = true;

                                    if (Success)
                                    {
                                        if (Other.ItemQuality(UppedItem2) == 3 || Other.ItemQuality(UppedItem2) == 4)
                                            UppedItem2 += 6 - (uint)Other.ItemQuality(UppedItem2);
                                        else
                                            UppedItem2 += 1;

                                        MyChar.RemoveItem((ulong)UppedItemUID);

                                        if (Splitter[4] == "0")
                                            if (Other.ChanceSuccess(1))
                                                Splitter[4] = "255";

                                        if (Splitter[5] == "0")
                                            if (Splitter[4] != "0")
                                                if (Other.ChanceSuccess(0.5))
                                                    Splitter[5] = "255";

                                        MyChar.AddItem(Convert.ToString(UppedItem2) + "-" + Splitter[1] + "-" + Splitter[2] + "-" + Splitter[3] + "-" + Splitter[4] + "-" + Splitter[5], 0, UppedItemUID);
                                    }

                                    MyChar.RemoveItem((uint)UppingItemUID);
                                }
                        
                        break;
                    }
                #endregion
                #region itemdrops
                case 27:
                    {
                        Send(Data);
                        break;
                    }
                case 3:
                    {

                        uint ItemUID = BitConverter.ToUInt32(Data, 4);//(uint)((Data[7] << 24) + (Data[6] << 16) + (Data[5] << 8) + Data[4]);

                        int Count = 0;

                        foreach (uint uid in MyChar.Inventory_UIDs)
                        {
                            if (uid == ItemUID && There == true)
                            {
                                string Item = MyChar.Inventory[Count];

                                DroppedItem e = DroppedItems.DropItem(Item, MyChar.LocX, MyChar.LocY, MyChar.LocMap, 0, MyChar);
                                World.ItemDrops(e);

                                MyChar.RemoveItem(ItemUID);
                            }
                            Count++;
                        }
                        
                        break;
                    }
                #endregion
                #region invitems
                case 2:
                    {

                        uint ItemUID = BitConverter.ToUInt32(Data, 8); //(uint)((Data[11] << 24) + (Data[10] << 16) + (Data[9] << 8) + Data[8]);
                        int Count = 0;
                        foreach (uint uid in MyChar.Inventory_UIDs)
                        {
                            if (uid == ItemUID)
                            {
                                string Item = MyChar.Inventory[Count];
                                string[] Splitter = Item.Split('-');
                                uint ItemId = uint.Parse(Splitter[0]);

                                foreach (uint[] item in DataBase.Items)
                                {
                                    if (item[0] == ItemId)
                                    {
                                        uint SellFor = item[7] / 3;

                                        MyChar.RemoveItem(ItemUID);
                                        MyChar.Silvers += SellFor;

                                        break;
                                    }
                                }
                                break;
                            }
                            Count++;
                        }
                        
                        break;
                    }
                #endregion
                #region shops
                case 1:
                    {

                        uint ItemID = BitConverter.ToUInt32(Data, 0x08);//(uint)((Data[0x0b] << 24) + (Data[0x0a] << 16) + (Data[0x09] << 8) + Data[0x08]);
                        uint CPsVal = Data[18];
                        uint Value = BitConverter.ToUInt32(Data, 0x04); //(uint)((Data[0x07] << 24) + (Data[0x06] << 16) + (Data[0x05] << 8) + Data[0x04]);
                        uint sValue = 0;
                        byte Amount = Data[20];
                        int Money = Data[5];
                        if (Amount == 0)
                            Amount = 1;

                        //if (ItemID == 118299 || ItemID == 160289 || ItemID == 2100089 || ItemID == 150289 || ItemID == 1088002 || ItemID == 1088003 || ItemID == 1088004 || ItemID == 2100025 || ItemID == 790001 || ItemID == 722550 || ItemID == 160269 || ItemID == 160279 || ItemID == 150269 || ItemID == 150279 || ItemID == 2100069 || ItemID == 2100079 || ItemID == 2100045 || ItemID == 723700 || ItemID == 723584 || ItemID >= 722840 && ItemID <= 722842 || ItemID >= 730001 && ItemID <= 730009 || ItemID == 1088000 || ItemID == 1088001 || ItemID >= 1080001 && ItemID <= 1080031 || ItemID == 720028 || ItemID == 720027 || ItemID == 720394 || ItemID == 723235 || ItemID == 723236 || ItemID == 700043 || ItemID >= 700001 && ItemID <= 700073 || ItemID == 720381 || ItemID == 723712 || ItemID == 150299 || ItemID == 2100099 || ItemID == 160299 || ItemID == 780000 || ItemID == 1091020)
                        //{
                        //    General.WriteLine("Tried to buy unavailable item : (" + ItemID + ") - " + MyChar.Name + "!");
                        //}
                        //else
                        //{
                        string TehShop = System.IO.File.ReadAllText(DataBase.DBPath + @"\Shop.dat");

                        try
                        {
                            if (Other.CharExist(Convert.ToString(ItemID), TehShop))
                            {
                                foreach (uint[] item in DataBase.Items)
                                {
                                    if (ItemID == item[0])
                                    {
                                        sValue = item[7];
                                    }
                                }
                                if (MyChar.ItemsInInventory > 39 || MyChar.LocMap == 1511 || MyChar.LocMap == 1005 || MyChar.LocMap == 1601 || MyChar.LocMap == 1505 || MyChar.LocMap == 1082)
                                {
                                }
                                else
                                {
                                    if (MyChar.Silvers >= sValue)
                                    {
                                        MyChar.Silvers -= sValue;
                                        if (MyChar.Silvers < 0)
                                            MyChar.Silvers = 0;

                                        MyChar.AddItem(Convert.ToString(ItemID) + "-0-0-0-0-0", 0, (uint)General.Rand.Next(10000000));
                                     }
                                    else
                                    {
                                        Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "You don't have " + sValue + " silvers.", 2005));
                                        break;
                                    }
                                }
                            }
                            else
                            {
                                General.WriteLine("There is no such item in Shop.dat (" + ItemID + ") - " + MyChar.Name + "!");
                            }
                        }
                        catch { }
                        break;
                    }
                #endregion
                #region equipt
                case 4:
                    {
                        ulong ItemUID = (ulong)BitConverter.ToUInt32(Data, 4);//(ulong)((Data[7] << 24) + (Data[6] << 16) + (Data[5] << 8) + Data[4]);
                        byte RequestPos = Data[8];

                        string TheItem = "";
                        uint TheUID = 0;
                        int count = 0;
                        uint ItemID = 0;

                        foreach (uint uid in MyChar.Inventory_UIDs)
                        {
                            if (ItemUID == uid)
                            {
                                TheUID = uid;
                                TheItem = MyChar.Inventory[count];
                                string[] Splitter = TheItem.Split('-');
                                ItemID = uint.Parse(Splitter[0]);
                            }
                            count++;
                        }
                        if (ItemID == 1050000 || ItemID == 1050001 || ItemID == 1050002)
                            RequestPos = 5;

                        if (RequestPos != 0)
                        {
                            if (Other.CanEquip(TheItem, MyChar))
                            {
                                if (Other.ItemType(ItemID) != 5 && RequestPos != 0)
                                {
                                    MyChar.RemoveItem(TheUID);
                                    if (MyChar.Equips[RequestPos] == null || MyChar.Equips[RequestPos] == "0")
                                    {
                                        MyChar.AddItem(TheItem, RequestPos, TheUID);
                                    }
                                    else
                                    {
                                        MyChar.UnEquip(RequestPos);
                                        MyChar.AddItem(TheItem, RequestPos, TheUID);
                                    }
                                    World.UpdateSpawn(MyChar);
                                }
                                else if (RequestPos != 0)
                                    if (MyChar.ItemsInInventory < 40)
                                    {
                                        if (MyChar.Equips[4] == null && MyChar.Equips[5] == null)
                                        {
                                            MyChar.AddItem(TheItem, RequestPos, TheUID);
                                            World.UpdateSpawn(MyChar);
                                            MyChar.RemoveItem(TheUID);
                                        }
                                        else
                                        {
                                            MyChar.RemoveItem(TheUID);
                                            if (MyChar.Equips[4] != null)
                                            {
                                                MyChar.UnEquip(4);
                                            }
                                            if (MyChar.Equips[5] != null)
                                            {
                                                MyChar.UnEquip(5);
                                            }
                                            MyChar.AddItem(TheItem, 4, TheUID);

                                            World.UpdateSpawn(MyChar);
                                        }
                                    }

                            }
                        }
                        else
                        {
                            MyChar.UseItem(TheUID, TheItem);
                        }
                        
                        break;
                    }
                case 6:
                    {
                        
                        if (MyChar.ItemsInInventory > 39)
                            return;

                        ulong ItemUID = (ulong)BitConverter.ToUInt32(Data, 4); //(ulong)((Data[7] << 24) + (Data[6] << 16) + (Data[5] << 8) + Data[4]);

                        int count = 0;
                        foreach (ulong uid in MyChar.Equips_UIDs)
                        {
                            if (uid == ItemUID)
                            {
                                MyChar.UnEquip((byte)count);
                            }
                            count++;
                        }

                        World.UpdateSpawn(MyChar);

                        
                        break;
                    }
            }

        }
        break;
                #endregion
                #region actions
                case 1010:
                    {
                        PacketType = Data[22];

                        switch (PacketType)
                        {
                            case 120:
                                {
                                    if (MyChar.Flying)
                                    {
                                        MyChar.Flying = false;
                                        Send(General.MyPackets.Vital(MyChar.UID, 26, MyChar.GetStat()));
                                    }
                                    break;
                                }
                            case 99:
                                {
                                    if (MyChar.LocMap == 1028)
                                        MyChar.Mining = true;
                                    break;
                                }
                            case 54:
                                {
                                    uint VUID = BitConverter.ToUInt32(Data, 12);//(uint)((Data[15] << 24) + (Data[14] << 16) + (Data[13] << 8) + Data[12]);
                                    Character ViewedChar = Kernel.Chars[VUID];
                                    break;
                                }
                            case 140:
                                {
                                    uint UID = BitConverter.ToUInt32(Data, 12);
                                    Character Char = Kernel.Chars[UID];
                                    if (Char != null)
                                        Send(General.MyPackets.FriendEnemyInfoPacket(Char, 0));
                                    break;
                                }
                            case 94:
                                {
                                    {
                                        MyChar.Revive(false);
                                    }
                                    break;
                                }
                            case 117:
                                {

                                    uint Value1 = BitConverter.ToUInt32(Data, 4);//(Data[7] << 24) + (Data[6] << 16) + (Data[5] << 8) + Data[4];
                                    uint CharID = BitConverter.ToUInt32(Data, 8);//(uint)((Data[11] << 24) + (Data[10] << 16) + (Data[9] << 8) + Data[8]);
                                    uint VUID = BitConverter.ToUInt32(Data, 12);//(uint)((Data[15] << 24) + (Data[14] << 16) + (Data[13] << 8) + Data[12]);

                                    Character ViewedChar = Kernel.Chars[VUID];
                                    string[] Splitter;
                                    Send(General.MyPackets.ViewEquip(ViewedChar));

                                    if (ViewedChar.Equips[1] != null && ViewedChar.Equips[1] != "0")
                                    {
                                        Splitter = ViewedChar.Equips[1].Split('-');
                                        Send(General.MyPackets.ViewEquipAdd(VUID, uint.Parse(Splitter[0]), byte.Parse(Splitter[1]), byte.Parse(Splitter[2]), byte.Parse(Splitter[3]), byte.Parse(Splitter[4]), byte.Parse(Splitter[5]), 1, 100, 100));
                                    }
                                    else
                                        Send(General.MyPackets.ViewEquipAdd(VUID, 0, 0, 0, 0, 0, 0, 1, 0, 0));

                                    if (ViewedChar.Equips[2] != null && ViewedChar.Equips[2] != "0")
                                    {
                                        Splitter = ViewedChar.Equips[2].Split('-');
                                        Send(General.MyPackets.ViewEquipAdd(VUID, uint.Parse(Splitter[0]), byte.Parse(Splitter[1]), byte.Parse(Splitter[2]), byte.Parse(Splitter[3]), byte.Parse(Splitter[4]), byte.Parse(Splitter[5]), 2, 100, 100));
                                    }
                                    else
                                        Send(General.MyPackets.ViewEquipAdd(VUID, 0, 0, 0, 0, 0, 0, 2, 0, 0));

                                    if (ViewedChar.Equips[3] != null && ViewedChar.Equips[3] != "0")
                                    {
                                        Splitter = ViewedChar.Equips[3].Split('-');
                                        Send(General.MyPackets.ViewEquipAdd(VUID, uint.Parse(Splitter[0]), byte.Parse(Splitter[1]), byte.Parse(Splitter[2]), byte.Parse(Splitter[3]), byte.Parse(Splitter[4]), byte.Parse(Splitter[5]), 3, 100, 100));
                                    }
                                    else
                                        Send(General.MyPackets.ViewEquipAdd(VUID, 0, 0, 0, 0, 0, 0, 3, 0, 0));

                                    if (ViewedChar.Equips[4] != null && ViewedChar.Equips[4] != "0")
                                    {
                                        Splitter = ViewedChar.Equips[4].Split('-');
                                        Send(General.MyPackets.ViewEquipAdd(VUID, uint.Parse(Splitter[0]), byte.Parse(Splitter[1]), byte.Parse(Splitter[2]), byte.Parse(Splitter[3]), byte.Parse(Splitter[4]), byte.Parse(Splitter[5]), 4, 100, 100));
                                    }
                                    else
                                        Send(General.MyPackets.ViewEquipAdd(VUID, 0, 0, 0, 0, 0, 0, 4, 0, 0));
                                    if (ViewedChar.Equips[5] != null && ViewedChar.Equips[5] != "0")
                                    {
                                        Splitter = ViewedChar.Equips[5].Split('-');
                                        Send(General.MyPackets.ViewEquipAdd(VUID, uint.Parse(Splitter[0]), byte.Parse(Splitter[1]), byte.Parse(Splitter[2]), byte.Parse(Splitter[3]), byte.Parse(Splitter[4]), byte.Parse(Splitter[5]), 5, 100, 100));
                                    }
                                    else
                                        Send(General.MyPackets.ViewEquipAdd(VUID, 0, 0, 0, 0, 0, 0, 5, 0, 0));

                                    if (ViewedChar.Equips[6] != null && ViewedChar.Equips[6] != "0")
                                    {
                                        Splitter = ViewedChar.Equips[6].Split('-');
                                        Send(General.MyPackets.ViewEquipAdd(VUID, uint.Parse(Splitter[0]), byte.Parse(Splitter[1]), byte.Parse(Splitter[2]), byte.Parse(Splitter[3]), byte.Parse(Splitter[4]), byte.Parse(Splitter[5]), 6, 100, 100));
                                    }
                                    else
                                        Send(General.MyPackets.ViewEquipAdd(VUID, 0, 0, 0, 0, 0, 0, 6, 0, 0));

                                    if (ViewedChar.Equips[7] != null && ViewedChar.Equips[7] != "0")
                                    {
                                        Splitter = ViewedChar.Equips[7].Split('-');
                                        Send(General.MyPackets.ViewEquipAdd(VUID, uint.Parse(Splitter[0]), byte.Parse(Splitter[1]), byte.Parse(Splitter[2]), byte.Parse(Splitter[3]), byte.Parse(Splitter[4]), byte.Parse(Splitter[5]), 7, 100, 100));
                                    }
                                    else
                                        Send(General.MyPackets.ViewEquipAdd(VUID, 0, 0, 0, 0, 0, 0, 7, 0, 0));

                                    if (ViewedChar.Equips[8] != null && ViewedChar.Equips[8] != "0")
                                    {
                                        Splitter = ViewedChar.Equips[8].Split('-');
                                        Send(General.MyPackets.ViewEquipAdd(VUID, uint.Parse(Splitter[0]), byte.Parse(Splitter[1]), byte.Parse(Splitter[2]), byte.Parse(Splitter[3]), byte.Parse(Splitter[4]), byte.Parse(Splitter[5]), 8, 100, 100));
                                    }
                                    else
                                        Send(General.MyPackets.ViewEquipAdd(VUID, 0, 0, 0, 0, 0, 0, 8, 0, 0));

                                    if (ViewedChar.Equips[9] != null && ViewedChar.Equips[9] != "0")
                                    {
                                        Splitter = ViewedChar.Equips[9].Split('-');
                                        Send(General.MyPackets.ViewEquipAdd(VUID, uint.Parse(Splitter[0]), byte.Parse(Splitter[1]), byte.Parse(Splitter[2]), byte.Parse(Splitter[3]), byte.Parse(Splitter[4]), byte.Parse(Splitter[5]), 9, 100, 100));
                                    }
                                    else
                                        Send(General.MyPackets.ViewEquipAdd(VUID, 0, 0, 0, 0, 0, 0, 9, 0, 0));


                                    
                                    break;
                                }
                            #region avatar
                            case 142:
                                {
                                    
                                    Send(Data);
                                    uint Face = Data[12];

                                    if (Face > 200)
                                        Face -= 200;

                                    uint Multiply = (uint)(Data[13] * 56);
                                    Face += Multiply;

                                    MyChar.Avatar = (byte)Face;
                                    MyChar.Silvers -= 500;
                                    World.UpdateSpawn(MyChar);
                                    
                                    break;
                                }
                            #endregion
                            #region mining
                            case 133:
                                {
                                    if (MyChar.Mining)
                                        MyChar.Mining = false;

                                    Send(Data);
                                    short PrevX = BitConverter.ToInt16(Data, 0x10);//(short)((Data[0x11] << 8) + Data[0x10]);
                                    short PrevY =BitConverter.ToInt16(Data, 0x12); //(short)((Data[0x13] << 8) + Data[0x12]);
                                    short NewX = BitConverter.ToInt16(Data, 0xC);//(short)((Data[0xd] << 8) + Data[0xc]);
                                    short NewY = BitConverter.ToInt16(Data, 0xE);//(short)((Data[0xf] << 8) + Data[0xe]);

                                    MyChar.Attacking = false;
                                    MyChar.TargetUID = 0;
                                    MyChar.MobTarget = null;
                                    MyChar.TGTarget = null;
                                    MyChar.PTarget = null;
                                    MyChar.SkillLooping = 0;
                                    MyChar.AtkType = 0;
                                    MyChar.PrevX = MyChar.LocX;
                                    MyChar.PrevY = MyChar.LocY;
                                    MyChar.LocX = (ushort)NewX;
                                    MyChar.LocY = (ushort)NewY;
                                    MyChar.Action = 100;

                                    World.SpawnMeToOthers(MyChar, true);
                                    World.SpawnOthersToMe(MyChar, true);
                                    World.PlayerMoves(MyChar, Data);
                                    World.SurroundNPCs(MyChar, true);
                                    World.SurroundMobs(MyChar, true);
                                    World.SurroundDroppedItems(MyChar, true);

                                    break;
                                }
                            #endregion
                            #region welcome
                            case 74:
                                {
                                    if (There)
                                        return;
                                    if (MyChar == null)
                                        return;
                                    
                                    Send(General.MyPackets.PlacePacket1(MyChar));
                                    Send(General.MyPackets.PlacePacket2(MyChar));
                                    Send(General.MyPackets.PlacePacket3(MyChar));
                                    Send(General.MyPackets.LogonPacket());
                                    Send(General.MyPackets.ShowMinimap(true));
                                    Send(General.MyPackets.GeneralData((long)(MyChar.UID), 3, 0, 0, 96));
                                    Send(General.MyPackets.CharacterInfo(MyChar));
                                    Send(General.MyPackets.Vital(MyChar.UID, 26, MyChar.GetStat()));
                                    MyChar.StartXPCircle();

                                    MyChar.PKMode = 3;

                                    World.SpawnMeToOthers(MyChar, false);
                                    World.SpawnOthersToMe(MyChar, false);
                                    World.SurroundNPCs(MyChar, false);
                                    World.SurroundMobs(MyChar, false);
                                    World.SurroundDroppedItems(MyChar, false);
                                    MyChar.UnPackInventory();
                                    MyChar.SendInventory();
                                    MyChar.UnPackEquips();
                                    MyChar.SendEquips(true);
                                    MyChar.UnPackSkills();
                                    MyChar.SendSkills();
                                    MyChar.UnPackProfs();
                                    MyChar.SendProfs();
                                    MyChar.UnPackWarehouses();
                                    MyChar.UnPackEnemies();
                                    MyChar.UnPackFriends();

                                    foreach (DictionaryEntry DE in Guilds.AllGuilds)
                                    {
                                        Guild AGuild = (Guild)DE.Value;
                                        Send(General.MyPackets.GuildName(AGuild.GuildID, AGuild.GuildName));
                                    }
                                    if (MyChar.MyGuild != null)
                                    {
                                        Send(General.MyPackets.GuildInfo(MyChar.MyGuild, MyChar));
                                        Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, MyChar.MyGuild.Bulletin, 2111));
                                        Send(General.MyPackets.GuildName(MyChar.GuildID, MyChar.MyGuild.GuildName));
                                        Send(General.MyPackets.GeneralData(MyChar.UID, 0, 0, 0, 97));
                                    }

                                    if (MyChar.RBCount == 2)
                                    {
                                        Send(General.MyPackets.String(MyChar.UID, 10, "2NDMetempsychosis"));
                                    }
                                    if (MyChar.Rank == 7)
                                        Send(General.MyPackets.String(MyChar.UID, 10, "letter1"));
                                    if (MyChar.Rank == 6)
                                        Send(General.MyPackets.String(MyChar.UID, 10, "letter2"));
                                    if (MyChar.Rank == 5)
                                        Send(General.MyPackets.String(MyChar.UID, 10, "letter3"));
                                    if (MyChar.Rank == 4)
                                        Send(General.MyPackets.String(MyChar.UID, 10, "letter4"));
                                    if (MyChar.Rank == 3)
                                        Send(General.MyPackets.String(MyChar.UID, 10, "letter5"));
                                    if (MyChar.Rank == 2)
                                    {
                                        Send(General.MyPackets.String(MyChar.UID, 10, "coronet3"));
                                        Send(General.MyPackets.String(MyChar.UID, 10, "letter6"));
                                    }
                                    if (MyChar.Rank == 1)
                                    {
                                        Send(General.MyPackets.String(MyChar.UID, 10, "coronet4"));
                                        Send(General.MyPackets.String(MyChar.UID, 10, "letter7"));
                                    }
                                    Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "[Sphinx Chronicles Online]", 2000));
                                    Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "We are currently under developement. You may see things being added", 2000));
                                    Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "as time goes on. Until then, please follow the thread on ePVP or our forum.", 2000));
                                    Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "Sphinx Revision: 1.0.1a", 2000));
                                    Send(General.MyPackets.SendMsg(UID, "SYSTEM", MyChar.Name, "Players Online: " + Kernel.Chars.Count, 2005));
                                    Send(General.MyPackets.GeneralData(MyChar.UID, 0, 0, 0, 75));
                                    There = true;
                                    General.WriteLine("Succesfully login to server. (" + MyChar.Name + " - " + Account + "[" + Ip + "]" + ")");
                                    World.SpawnMeToOthers(MyChar, true);
                                    Kernel.Chars.Add(this.UID, this.MyChar);
                                    World.SpawnMeToOthers(MyChar, false);
                                    break;
                                }
                            #endregion
                            #region pvp
                            case 96:
                                {
                                    
                                    Send(General.MyPackets.GeneralData((long)(MyChar.UID), Data[12], 0, 0, 96));
                                    MyChar.PKMode = Data[12];
                                    
                                    break;
                                }
                            #endregion
                            #region moremovement
                            case 79:
                                {
                                    
                                    MyChar.Direction = Data[20];
                                    World.PlayerMoves(MyChar, Data);
                                    
                                    break;
                                }
                            case 81:
                                {
                                    
                                    MyChar.Action = Data[12];
                                    World.PlayerMoves(MyChar, Data);
                                    
                                    break;
                                }
                            case 85:
                                {
                                    ulong CharID = BitConverter.ToUInt64(Data, 8);//(ulong)((Data[11] << 24) + (Data[10] << 16) + (Data[9] << 8) + (Data[8]));
                                    int x = BitConverter.ToInt32(Data, 12);//(Data[13] << 8) + (Data[12]);
                                    int y = BitConverter.ToInt32(Data, 14);//(Data[15] << 8) + (Data[14]);

                                    if (MyChar.UID == CharID)
                                    {
                                        if (!There)
                                            return;
                                        ushort NewMap = MyChar.LocMap;
                                        ushort NewX = (ushort)(MyChar.LocX + 3);
                                        ushort NewY = (ushort)(MyChar.LocY - 2);

                                        foreach (ushort[] p in DataBase.Portals)
                                        {
                                            if (p[0] == MyChar.LocMap)
                                                if (MyMath.PointDistance(MyChar.LocX, MyChar.LocY, p[1], p[2]) <= 6)
                                                {
                                                    NewMap = p[3];
                                                    NewX = p[4];
                                                    NewY = p[5];
                                                }
                                        }
                                        MyChar.Teleport(NewMap, NewX, NewY);
                                    }
                                    break;
                                }
                        }
                        break;
                    }
                            #endregion
                #endregion
                case 1001:
                    {
                        uint Model = 0;
                        uint Avatar = 0;
                        byte Class = 0;
                        string CharName = "";
                        bool ValidName = true;

                        Model = (uint)Data[0x35];
                        Model = (Model << 8) | (uint)(Data[0x34]);

                        Class = Data[0x36];

                        int x = 0x14;
                        while (x < 0x24 && Data[x] != 0x00)
                        {
                            CharName += Convert.ToChar(Data[x]);
                            x++;
                        }
                        if (Model == 1003)
                            Avatar = 67;
                        else if (Model == 1004)
                            Avatar = 67;
                        else if (Model == 2001)
                            Avatar = 201;
                        else if (Model == 2002)
                            Avatar = 201;

                        if (CharName.IndexOfAny(new char[3] { ' ', '[', ']' }) > -1)
                        {
                            ValidName = false;
                        }

                        foreach (string name in DataBase.ForbiddenNames)
                        {
                            if (name == CharName)
                            {
                                ValidName = false;
                                break;
                            }
                        }

                        if (CharName.IndexOfAny(new char[3] { ' ', '[', ']' }) > -1)
                        {
                            ValidName = false;
                        }

                        foreach (string name in DataBase.ForbiddenNames)
                        {
                            if (name == CharName)
                            {
                                ValidName = false;
                                break;
                            }
                        }

                        try
                        {
                            if (ValidName)
                            {
                                bool Success = DataBase.CreateCharacter(CharName, Class, Model, Avatar, this);
                                if (Success)
                                    Console.WriteLine("New character: " + CharName);
                                Online = false;
                                MySocket.Disconnect();
                            }
                            else
                            {
                                Online = false;
                                MySocket.Disconnect();
                            }
                        }
                        catch
                        {
                            MySocket.Disconnect();
                            return;
                        }
                    }
                    break;
            }
        }
        #region misc

        public void Send(byte[] Dat)
        {
            if (MySocket != null)
            {
                lock (MySocket)
                {
                    MySocket.Send(Dat);
                }
            }
        }

        public void Drop()
        {
            try
            {
                if (Online)
                {
                    Online = false;
                    //DataBase.ChangeOnlineStatus(Account, 0);             

                    if (MyChar != null)
                    {
                        World.RemoveEntity(MyChar);
                        if (MyChar.LocMap == 700)
                        {
                            MyChar.CPs += 27;
                            MyChar.Teleport(1036, 200, 200);
                        }

                        foreach (DictionaryEntry DE in MyChar.Friends)
                        {
                            uint FriendID = (uint)DE.Key;
                            if (Kernel.Chars.ContainsKey(FriendID))
                            {
                                Character Friend = Kernel.Chars[FriendID];
                                if (Friend != null)
                                {
                                    Friend.MyClient.Send(General.MyPackets.FriendEnemyPacket(MyChar.UID, MyChar.Name, 14, 0));
                                    Friend.MyClient.Send(General.MyPackets.FriendEnemyPacket(MyChar.UID, MyChar.Name, 15, 0));
                                    Friend.MyClient.Send(General.MyPackets.SendMsg(Friend.MyClient.UID, "SYSTEM", Friend.Name, "Your friend " + MyChar.Name + " has logged out.", 2005));
                                }
                            }
                        }

                        if (MyChar.Trading)
                        {
                            Character Who = Kernel.Chars[MyChar.TradingWith];
                            Who.MyClient.Send(General.MyPackets.TradePacket(MyChar.TradingWith, 5));
                            Who.TradingSilvers = 0;
                            Who.TradingCPs = 0;
                            Who.TradeOK = false;
                            Who.Trading = false;
                            MyChar.TradingWith = 0;
                            Who.MyClient.Send(General.MyPackets.SendMsg(Who.MyClient.UID, "SYSTEM", Who.Name, "Trading failed!", 2005));
                        }

                        if (MyChar.TeamLeader)
                            MyChar.TeamDismiss();

                        if (MyChar.MyTeamLeader != null && !MyChar.TeamLeader)
                            MyChar.MyTeamLeader.TeamRemove(MyChar, false);
                        
                        try
                        {
                            if (Kernel.Chars.ContainsKey(MyChar.UID))
                                Kernel.Chars.Remove(MyChar.UID);                          
                        }
                        catch (Exception Exc) { General.WriteLine(Exc.ToString()); }


                        DataBase.LogoutSave(MyChar);
                        MyChar.Trading = false;
                        MyChar.TradingSilvers = 0;
                        MyChar.TradingCPs = 0;
                        MyChar.TradeOK = false;
                        MyChar.Trading = false;
                        MyChar.AtkType = 0;
                        MyChar.JoinForbidden = false;
                        MyChar.SkillLoopingTarget = 0;
                        MyChar.SkillLoopingX = 0;
                        MyChar.SkillLoopingY = 0;
                        MyChar.SkillLooping = 0;
                        MyChar.SMOn = false;
                        MyChar.CycloneOn = false; 
                        MyChar.CastingPray = false;
                        Kernel.Chars.Remove(MyChar.UID);
                        MyChar = null;
                    }
                    MySocket.Disconnect();                    
                    MySocket = null;
                }
            }
            catch (Exception Exc) { General.WriteLine(Convert.ToString(Exc)); }
        }
        #endregion
    }
}